James Hallen
James Hallen

Reputation: 5014

Meaning of regex Python

Is the meaning of this regex: (\d+).*? - group a set of numbers, then take whatever that comes after (only one occurance of it at maximum, except a newline)?

Is there a difference in: (\d+) and [\d]+?

Upvotes: 0

Views: 99

Answers (3)

pypat
pypat

Reputation: 1116

group1 will be at least one number and group0 will contain group1 and maybe other characters but not necessarily.

edit to answer the edited question: AFAIK there should be no difference in the matching between those 2 other than the grouping.

Upvotes: 0

jamylak
jamylak

Reputation: 133634

Take as many digits as possible (at least 1), then take the smallest amount of characters as possible (except newline). The non greedy qualifier (?) doesn't really help unless you have the rest of your pattern following it, otherwise it will just match as little as possible, in this case, always 0.

>>> import re
>>> re.match(r'(\d+).*?', '123').group()
'123'
>>> re.match(r'(\d+).*?', '123abc').group()
'123'

The difference between (\d+) and [\d]+ is the fact that the former groups and the latter doesn't. ([\d]+) would however be equivalent.

>>> re.match(r'(\d+)', '123abc').groups()
('123',)
>>> re.match(r'[\d]+', '123abc').groups()
()

Upvotes: 2

DhruvPathak
DhruvPathak

Reputation: 43245

(\d)+  One or more occurance of digits, 
.* followed by any characters,
? lazy operator i.e. return the minimum match.

Upvotes: 0

Related Questions