1

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

1
  • @Simon yes, I included now Commented Mar 11, 2018 at 0:31

2 Answers 2

3

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)
Sign up to request clarification or add additional context in comments.

5 Comments

Can you answer by using non capturing group for the letters 'iama'
@AdityaMatturi: added one case else
(?!:iama)\d+ but it doesn't need at all
yeah, but I wanted more practice on non capturing. Just figuring out how to do with it
(?!:iama)\d+?(?=number) or (?!:iama)\d*?(?=number) without +, or else (?!:\D*)\d*?(?=\D*$)
3
>>> print(re.search(r'([0-9]+)','iama5559348number').groups()[0])
'5559348'

or

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

2 Comments

I appreciate your answer but can you use non capturing group(?:) or [^ ] and also use '*' instead of '+' in the answer
Just dunno how to do it without capture

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.