reusing parts of a string in RE matches?

John Salerno johnjsal at NOSPAMgmail.com
Wed May 10 10:29:12 EDT 2006


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? I figure it has something to do 
with look-ahead and look-behind, but I've only gotten this far:

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

This matches all the 'ab' followed by an 'a', but it doesn't include the 
'a'. What I'd like to do is find all the 'aba' matches. A regular 
findall() gives four results, but really there are seven.

Is there a way to do this with just an RE pattern, or would I have to 
manually add the 'a' to the end of the matches?

Thanks.



More information about the Python-list mailing list