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

Licheng Fang fanglicheng at gmail.com
Tue Sep 12 01:51:38 EDT 2006


Oh, please do have a look at the second link I've posted. There's a
table comparing the regexp engines. The engines you've tested probably
all use an NFA implementation.

MonkeeSage wrote:
> Licheng Fang wrote:
> > Hi, according to these regexp engine discussions, it's NOT a behavior
> > true to any implementation.
> > [snip]
>
> Well, I just double-checked in ruby (oniguruma regexp engine):
>
> r = Regexp.new("do|dolittle")
> puts r.match("dolittle")[0]
> # do
>
> r = Regexp.new("one(self)?(sufficient)?")
> puts r.match("oneselfsufficient")[0]
> # oneself
>
> And perl:
>
> if ("doolittle" =~
>     /(do|dolittle)/) {
>   print "$1\n";
>   # do
> }
>
> if ("oneselfsufficient" =~
>     /(one(self)?(selfsufficient)?)/) {
>   print "$1\n";
>   # oneself
> }
>
> And Javascript (whatever regexp engine Spidermonkey uses):
>
> var r = new RegExp(/do|dolittle/);
> alert("dolittle".match(r)[0]);
>
> var r = new RegExp(/one(self)?(selfsufficient)?/);
> alert("oneselfsufficient".match(r)[0]);
>
> So, it seems they are all broken, or python is correct as well.
> 
> Regards,
> Jordan




More information about the Python-list mailing list