473,399 Members | 2,478 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Overlapping matches in Regular Expressions


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?
Jul 18 '05 #1
1 3233
André Søreng wrote:
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>

Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: weston | last post by:
Has anyone ever encountered trouble with regular expressions not capturing matches specified by parentheses? I seem to have a weird situation where a regular expression is matching the String I'm...
5
by: Mystilleef | last post by:
Hello, Is there a simple flag to set to allow overlapping matches for the findall() regular expression method? In other words, if a string contains five occurrences of the string pattern "cat",...
9
by: Pete Davis | last post by:
I'm using regular expressions to extract some data and some links from some web pages. I download the page and then I want to get a list of certain links. For building regular expressions, I use...
3
by: Johhny | last post by:
Hello, I have recently written a small function that will verify that an IP address is valid. ==SNIP== import re ipAddress = raw_input('IP Address : ')
5
by: Chris Lasher | last post by:
Hey guys and gals, This is a followup of my "Counting all permutations of a substring" thread (see...
20
Samji
by: Samji | last post by:
Hi, everyone. I have this code when I'm reading in a file: // read lines into array var line = {}, lines = , hasmore; do { hasmore = istream.readLine(line); ...
1
by: Allan Ebdrup | last post by:
I have a dynamic list of regular expressions, the expressions don't change very often but they can change. And I have a single string that I want to match the regular expressions against and find...
6
by: Jeff | last post by:
I always have trouble with javascript regexes... I want to parse apart a string and remember the matches and use them elswhere. Say I have: var test_string='one_two-three'; In perl I...
8
by: Avi | last post by:
Hi all, I'm using string Replace(string oldValue, string newValue) and would like it to replace only full words that matches oldValue and not when oldValue is a substring of a larger word. ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.