leftmost longest match (of disjunctions) ; greediness of "|"

Joerg Schuster js at cis.uni-muenchen.de
Tue Dec 2 06:42:07 EST 2003


Padraig at Linux.ie writes:

> Joerg Schuster wrote:
> > Peter Hansen <peter at engcorp.com> writes:
> >
> >> produce a longer overall match.  In other words, the "|" operator
> >> is never greedy.
> > O.k. Thanks for pointing this out. Maybe I should have formulated my
> > question differently: Is there a trick (be it dirty or not) to make
> > "|" greedy in python?
> 
> sort the re by size first?

The point is not to get the match of the longest part of the
disjunction, but to get the match of that part of the disjunction
which is the longest one. (The match of ".*" may be much longer than
the match of "abc", although the latter regex contains more
characters.)

Jörg









More information about the Python-list mailing list