Overlapping matches

R

Rehceb Rotkiv

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
 
A

Ant

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
 
A

attn.steven.kuo

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
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top