Aditya Matturi
Aditya Matturi

Reputation: 119

extract number from string using regular expression

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

Answers (2)

TigerTV.ru
TigerTV.ru

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

Gilles Quénot
Gilles Quénot

Reputation: 185015

>>> print(re.search(r'([0-9]+)','iama5559348number').groups()[0])
'5559348'

or

>>> print(re.search(r'(\d+)','iama5559348number').groups()[0])
'5559348'

Upvotes: 3

Related Questions