flexible find and replace ?

OdarR Olivier.Darge at gmail.com
Mon Feb 16 21:10:22 CET 2009


Hi guys,

how would you do a clever find and replace, where the value replacing
the tag
is changing on each occurence ?

".......TAG............TAG................TAG..........TAG....."

is replaced by this :

".......REPL01............REPL02................REPL03..........REPL04..."


A better and clever method than this snippet should exist I hope :

counter = 1
while 'TAG' in mystring:
    mystring=mystring.replace('TAG', 'REPL'+str(counter), 1)
    counter+=1
    ...

(the find is always re-starting at the string beginning, this is not
efficient.

any ideas ? thanks,

Olivier



More information about the Python-list mailing list