Reputation: 119
I need to extract number from string 'iama5559348number'. Answer should do non capturing and expected output is 5559348. I am trying by doing this way
print(re.search(r'([^(.*)]?[0-9]*)','iama5559348number'))
but I am getting only only first letter
Upvotes: 1
Views: 84
Reputation: 1086
You can use re.findall
that way:
x= re.findall(r'[0-9]+','iama5559348number')[0]
print(x)
or that:
x= re.findall(r'\d+','iama5559348number')[0]
print(x)
Upvotes: 3
Reputation: 185015
>>> print(re.search(r'([0-9]+)','iama5559348number').groups()[0])
'5559348'
or
>>> print(re.search(r'(\d+)','iama5559348number').groups()[0])
'5559348'
Upvotes: 3