Overlapping matches

attn.steven.kuo at gmail.com attn.steven.kuo at gmail.com
Sun Apr 1 18:21:24 EDT 2007


On Apr 1, 1:38 pm, Rehceb Rotkiv <reh... at no.spam.plz> wrote:
> 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




More information about the Python-list mailing list