reusing parts of a string in RE matches?

Murali maha.murali at gmail.com
Thu May 11 04:10:13 CEST 2006


> Yes, and no extra for loops are needed!  You can define groups inside
> the lookahead assertion:
>
>   >>> import re
>   >>> re.findall(r'(?=(aba))', 'abababababababab')
>   ['aba', 'aba', 'aba', 'aba', 'aba', 'aba', 'aba']

Wonderful and this works with any regexp, so

import re

def all_occurences(pat,str):
  return re.findall(r'(?=(%s))'%pat,str)

all_occurences("a.a","abacadabcda") returns ["aba","aca","ada"] as
required.

- Murali




More information about the Python-list mailing list