I have a couple of string and I want to split them based on the numbers in it.
Ex:
In all these cases I want to extract the numbers that is 5, 10 and 16. The numbers could be up to infinity.
This is because I want to extract numbers and compare the length of the string whether they are equal or not.
>>> import re
>>> re.findall(r'\d+', '5Hello')
[5]
>>> re.findall(r'\d+', '10HelloWorld')
[10]
Edit: Answer to the specific question
import re
def check_len(x):
match = re.search(r'(\d+)(\w+)', x)
return int(match[1])==len(match[2])
check_len('5hello')
True
check_len('4rabbits')
False
are you looking for something like this
import re
def get_num(raw_str):
number = re.search(r'\d+',raw_str)
return number.group(0)
print(get_num('5hello'))
print(get_num('10HelloWorld'))
print(get_num('16HelloWorldPython'))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.