reusing parts of a string in RE matches?
Murali
maha.murali at gmail.com
Wed May 10 10:52:42 EDT 2006
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
More information about the Python-list
mailing list