flexible find and replace ?
bearophileHUGS at lycos.com
bearophileHUGS at lycos.com
Mon Feb 16 15:22:40 EST 2009
OdarR:
> 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..."
You may use something like this (tested) replacing XXXXXXXXXXXX with
the correct re function:
import re
def replacer(mobj):
replacer.counter += 1
return "REPL%02d" % replacer.counter
replacer.counter = 0
print re.XXXXXXXXXXXX("TAG", replacer, s1)
(If you have more than 99 replacements it will use more than two
digits.)
Bye,
bearophile
More information about the Python-list
mailing list