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

MonkeeSage MonkeeSage at gmail.com
Tue Sep 12 01:19:05 EDT 2006


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