xinnjie
xinnjie

Reputation: 732

matching number in python

I'm trying to match numbers with regex in python3.5 while re.match() works well, like this:

re.match(r"\d+(\.\d+)?", "12323.3 + 232131.2")
>>><_sre.SRE_Match object; span=(0, 7), match='12323.3'>

re.findall() did not return what I expect(I want ["12323.3","232131.2"]):

 re.findall(r"\d+(\.\d+)?", "12323.3 + 232131.2")
 >>>['.3', '.2']

please someone tell me why. Thanks.

Upvotes: 0

Views: 58

Answers (1)

ysth
ysth

Reputation: 98423

If there are capturing parentheses, findall returns all captured groups. You are only capturing the portion beginning with the ..

Try: r"(\d+(?:\.\d+)?)"

or capture nothing:

r"\d+(?:\.\d+)?"

Upvotes: 3

Related Questions