How to get the "longest possible" match with Python's RE module?

Tim Peters tim.peters at gmail.com
Wed Sep 13 04:16:43 CEST 2006


[Licheng Fang]
>> Basically, the problem is this:
>>
>> >>> p = re.compile("do|dolittle")
>> >>> p.match("dolittle").group()
>> 'do'

...

>> 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?

[Bryan Olson]
> Yes. Here's a way, but it sucks real bad:
>
>
>      def longest_match(re_string, text):
>         regexp = re.compile('(?:' + re_string + ')$')
>          while text:
>             m = regexp.match(text)
>              if m:
>                  return m
>              text = text[:-1]
>          return None

If you want to try something like that, note that the match() method
accepts optional slice arguments, so the "text = text[:-1]" business
can be replaced with much quicker little-integer arithmetic.



More information about the Python-list mailing list