How to get the "longest possible" match with Python's RE module?
kondal
kondal04 at gmail.com
Tue Sep 12 00:05:46 EDT 2006
Licheng Fang wrote:
> Basically, the problem is this:
>
> >>> p = re.compile("do|dolittle")
> >>> p.match("dolittle").group()
> 'do'
>
> Python's NFA regexp engine trys only the first option, and happily
> rests on that. There's another example:
>
> >>> p = re.compile("one(self)?(selfsufficient)?")
> >>> p.match("oneselfsufficient").group()
> 'oneself'
>
> The Python regular expression engine doesn't exaust all the
> possibilities, but in my application I hope to get the longest possible
> match, starting from a given point.
>
> Is there a way to do this in Python?
This is the way the regexp works python doesn't has anything to do with
it. It starts parsing the data with the pattern given. It returns the
matched string acording the pattern and doesn't go back to find the
other combinations.
So to get all the combinations you would probably require to give
different patterns each time.
More information about the Python-list
mailing list