Reputation: 5014
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
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
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
Reputation: 43245
(\d)+ One or more occurance of digits,
.* followed by any characters,
? lazy operator i.e. return the minimum match.
Upvotes: 0