reusing parts of a string in RE matches?

John Salerno johnjsal at NOSPAMgmail.com
Wed May 10 15:37:46 EDT 2006


John Salerno wrote:
> mpeters42 at gmail.com wrote:
> 
>>>>> string = 'abababababababab'
>>>>> pat = 'aba'
>>>>> [pat for s in re.compile('(?='+pat+')').findall(string)]
>> ['aba', 'aba', 'aba', 'aba', 'aba', 'aba', 'aba']
> 
> Wow, I have no idea how to read that RE. First off, what does it match? 
> Should something come before the parentheses, and that will be what 
> matches? Also, what are the '+'s doing? Are they literal +s or still 
> being used as RE syntax?

Nevermind, I get it! The point is that you *aren'* matching anything 
(except the empty string), and this only to see how many times it 
occurs, then you are replacing those occurrences with the pattern 
string. So this is basically like a counter?



More information about the Python-list mailing list