473,406 Members | 2,620 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,406 software developers and data experts.

Overlapping matches

In the re documentation, it says that the matching functions return "non-
overlapping" matches only, but I also need overlapping ones. Does anyone
know how this can be done?

Regards,
Rehceb Rotkiv
Apr 1 '07 #1
3 1454
Ant
On Apr 1, 9:38 pm, Rehceb Rotkiv <reh...@no.spam.plzwrote:
In the re documentation, it says that the matching functions return "non-
overlapping" matches only, but I also need overlapping ones. Does anyone
know how this can be done?
Something like the following:

import re

s = "oooooooo"
p = re.compile("oo")
out = []

while pos < endpos:
m = p.search(s, pos)
if not m:
break
out.append(m)
pos = m.start() + 1
Apr 1 '07 #2
On Apr 1, 1:38 pm, Rehceb Rotkiv <reh...@no.spam.plzwrote:
In the re documentation, it says that the matching functions return "non-
overlapping" matches only, but I also need overlapping ones. Does anyone
know how this can be done?

Perhaps lookahead assertions are what you're
looking for?

import re
import string

non_overlap = re.compile(r'[0-9a-fA-F]{2}')
pairs = [ match.group(0) for match in
non_overlap.finditer(string.hexdigits) ]
print pairs

overlap = re.compile(r'[0-9a-fA-F](?=([0-9a-fA-F]))')
pairs = [ match.group(0) + match.group(1) for match in
overlap.finditer(string.hexdigits) ]
print pairs

--
Hope this helps,
Steven

Apr 1 '07 #3
Both methods work well, thank you!
Apr 2 '07 #4

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

Similar topics

1
by: km | last post by:
Hi all, python re module deals with only nonoverlapping matches. how to go for if i want to find out overlapping matches and their span ? regards, KM
1
by: André Søreng | last post by:
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) .......
11
by: Max M | last post by:
I am writing a "find-free-time" function for a calendar. There are a lot of time spans with start end times, some overlapping, some not. To find the free time spans, I first need to convert the...
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",...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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.