reusing parts of a string in RE matches?

Murali maha.murali at gmail.com
Wed May 10 22:10:13 EDT 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