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

Frederic Rentsch anthra.norell at vtxmail.ch
Wed Sep 13 16:40:04 CEST 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
>
>   
Very true! Funny you should remind me, considering that I spent quite 
some time upgrading SE to allow regular expressions. Version 1 didn't 
and could resolve precedence at compile time. Version 2 resolves 
precedence at runtime by length of the matches and should function 
correctly in this respect, although it might not function fast enough 
for speed-critical applications. But then there is in general a 
trade-off between convenience and speed.


Frederic




More information about the Python-list mailing list