reusing parts of a string in RE matches?

Kent Johnson kent at kentsjohnson.com
Wed May 10 21:47:27 CEST 2006


John Salerno wrote:
> I probably should find an RE group to post to, but my news server at 
> work doesn't seem to have one, so I apologize. But this is in Python 
> anyway :)
> 
> So my question is, how can find all occurrences of a pattern in a 
> string, including overlapping matches? 

You can specify a start location to re.search(), and get the location of 
a match from a match object. This allows you to loop, searching the 
string following the last match:

import re
string = 'abababababababab'
pattern = re.compile(r'ab(?=a)')

ans = []
start = 0
while True:
     m = pattern.search(string, start)
     if not m: break
     ans.append( (m.start(), m.end()) )
     start = m.start() + 1

print ans # => [(0, 2), (2, 4), (4, 6), (6, 8), (8, 10), (10, 12), (12, 14)]

Kent



More information about the Python-list mailing list