Home > Back-end >  How can I select from my_str only numbers 10,20,30,40 with string regex?
How can I select from my_str only numbers 10,20,30,40 with string regex?

Time:02-15

my_str = "10 abc 20 de44 30 55fg 40"
ret = re.compile(r"\d{2}")
print(ret.findall(my_str))

CodePudding user response:

One digit followed by 0 (10 to 90):

ret = re.compile(r"\d{1}0")

Only 10, 20, 30 and 40:

ret = re.compile(r"[1234]0")

CodePudding user response:

An example on the Python regex page can be adapted to work with your use case.

re.findall(r'\b\d \b', my_str)

This should select all standalone numbers in your string.

  • Related