reusing parts of a string in RE matches?

mpeters42 at gmail.com mpeters42 at gmail.com
Wed May 10 14:52:43 EDT 2006


>From the Python 2.4 docs:

  findall( pattern, string[, flags])
  Return a list of all ***non-overlapping*** matches of pattern in
string....

By design, the regex functions return non-overlapping patterns.

Without doing some kind of looping, I think you are out of luck.

If you pattern is fixed, then a solution might be:
>>>string = 'abababababababab'
>>>pat = 'aba'
>>>[pat for s in re.compile('(?='+pat+')').findall(string)]
['aba', 'aba', 'aba', 'aba', 'aba', 'aba', 'aba']

If the pattern is not fixed (i.e. 'a.a') then this method can still get
a count of overlapping matches, but cannot get the individual match
strings themselves.

A simple loop should do in this case, though:

>>> for i in range(len(string)):
...     r= re.match(pat,string[i:])
...     if r: print r.group()
...
aba
aba
aba
aba
aba
aba
aba




More information about the Python-list mailing list