Overlapping matches in Regular Expressions

André Søreng andreis at stud.cs.uit.no
Tue Apr 12 06:23:18 EDT 2005


With the re/sre module included with Python 2.4:

pattern = "(?P<id1>avi)|(?P<id2>avi|mp3)"
string2match = "some string with avi in it"
matches = re.finditer(pattern, string2match)
...
matches[0].groupdict()
{'id2': None, 'id1': 'avi'}

Which was expected since overlapping matches are ignored.
But I would also like to know if other groups had a match.
What modifications to the re/sre module is needed to allow
overlapping matches?



More information about the Python-list mailing list