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
-
@Simon yes, I included nowAditya Matturi– Aditya Matturi2018-03-11 00:31:57 +00:00Commented Mar 11, 2018 at 0:31
Add a comment
|
2 Answers
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)
5 Comments
Aditya Matturi
Can you answer by using non capturing group for the letters 'iama'
TigerTV.ru
@AdityaMatturi: added one case else
TigerTV.ru
(?!:iama)\d+ but it doesn't need at allAditya Matturi
yeah, but I wanted more practice on non capturing. Just figuring out how to do with it
TigerTV.ru
(?!:iama)\d+?(?=number) or (?!:iama)\d*?(?=number) without +, or else (?!:\D*)\d*?(?=\D*$)>>> print(re.search(r'([0-9]+)','iama5559348number').groups()[0])
'5559348'
or
>>> print(re.search(r'(\d+)','iama5559348number').groups()[0])
'5559348'
2 Comments
Aditya Matturi
I appreciate your answer but can you use non capturing group(?:) or [^ ] and also use '*' instead of '+' in the answer
Gilles Quénot
Just dunno how to do it without capture