Reputation: 11122
whats the difference between '{m}' and '{m,n}?' in http://docs.python.org/library/re.html it says '{m,n}?' matches numbers in range m to n times, but it is not a greedy search. Therefore if its not a greedy search wouldn't it only match up to m no matter what?
Upvotes: 3
Views: 1771
Reputation: 57544
It's easiest to see with an example using two matching groups:
>>> re.match(r'(x{1,3}?)(.*)', 'xxxxx').groups()
('x', 'xxxx')
>>> re.match(r'(x{1,3})(.*)', 'xxxxx').groups()
('xxx', 'xx')
In other words, {n,m} and {n,m}? are both able to match exactly the same things; what it changes is where the groupings happen when there's more than one way to match.
Upvotes: 1
Reputation: 336478
{m,n}?
will preferably match only m
repetitions, but it will expand as needed up to n
repetitions if that's necessary for a longer match.
Compare ^x{2}y$
and ^x{2,4}?y$
:
The former will fail on xxxy
whereas the latter will match.
To summarize:
x{m}
: Match x
exactly m
times.
x{m,n}
: Try to match x
n
times, but if that causes the overall match to fail, give back as needed, but match at least m
times (greedy quantifier).
x{m,n}?
: Try to match x
m
times, but if that causes the overall match to fail, expand as needed, but match at most n
times (lazy quantifier).
Upvotes: 14