John Salerno wrote: > 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) >
Why not something like import re string = 'abababababababab' pattern = re.compile(r"^aba") ans = [] for x in xrange(len(string)): m = pattern.match(string[x:]) if m: ans.append( (x+m.start(),x+m.end())) # now ans is a list of pairs (p,q) where the substring string[p:q] matches pattern - Murali -- http://mail.python.org/mailman/listinfo/python-list