re.match -- not greedy?

Ant antroy at gmail.com
Wed Nov 22 03:26:49 EST 2006


EXI-Andrews, Jack wrote:

> the '*' and '+' don't seem to be greedy.. they will consume less in
> order to match a string:
>
> >>> import re;re.match('(a+)(ab)','aaab').groups()
> ('aa', 'ab')
>
> this is the sort of behaviour i'd expect from
>    '(a+?)(ab)'
>
> a+ should greedily consume a's at the expense of the string not matching

They are greedy - they consume as much as possible *without*
sacrificing the match.

You are thinking I think of possessive quantifiers, such as found in
Java, which *will* match as much as possible even if doing so causes
the match to fail. This would be written:

re.match('(a++)(ab)','aaab')

Python doesn't support these possessive quantifiers.




More information about the Python-list mailing list