Overlapping matches in Regular Expressions

  • Thread starter =?ISO-8859-1?Q?Andr=E9_S=F8reng?=
  • Start date
?

=?ISO-8859-1?Q?Andr=E9_S=F8reng?=

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?
 
F

Fredrik Lundh

André Søreng said:
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.

that's not how regular expressions work: a regular expression describes a
set of strings (the regular set), and the engine can tell you if a given string
belongs to that set.
What modifications to the re/sre module is needed to allow
overlapping matches?

if you want overlapping matches, you have to apply the pattern multiple
times. for trivial cases like your example, it's probably easier to create a
single pattern that matches all interesting cases, and use a dictionary (or
a number of sets) to do the rest.

</F>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top