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

Frederic Rentsch anthra.norell at vtxmail.ch
Wed Sep 13 10:45:35 EDT 2006


gatti at dsdata.it wrote:
> Frederic Rentsch wrote:
>
>
>   
>>    If you need regexes, why not just reverse-sort your expressions? This
>> seems a lot easier and faster than writing another regex compiler.
>> Reverse-sorting places the longer ones ahead of the shorter ones.
>>     
>
> Unfortunately, not all regular expressions have a fixed match length.
> Which is the longest of, for example, /(abc)?def/ and /(def)?ghi/
> depends on the input. 
>
> Lorenzo Gatti
>
>   
Oh yes, and my proposal to reverse-sort the targets was in response to 
the OP who wanted to generate a regex from a bunch of strings. It should 
work for that.


Frederic




More information about the Python-list mailing list