leftmost longest match (of disjunctions)

Joerg Schuster js at cis.uni-muenchen.de
Mon Dec 1 12:11:28 EST 2003


Hello,

The program given below returns the lines:

a
ab

Is there a way to use python regular expressions such that the program
would return the following lines?

ab
ab

########################################################################

import re

rx1 = re.compile("(a|ab)")
rx2 = re.compile("(ab|a)")

out1 = rx1.search("ab")
out2 = rx2.search("ab")

print out1.group(1)
print out2.group(1)

########################################################################

Jörg




More information about the Python-list mailing list