Multiple string.replace in one run
Thomas Güttler
guettler at thomas-guettler.de
Fri Jun 20 09:43:57 EDT 2003
John Hunter wrote:
>>>>>> "Thomas" == Thomas Güttler <guettler at thomas-guettler.de> writes:
>
> Thomas> I could do this with string.replace, but if I want to
> Thomas> replace several variables, I need to do the replace
> Thomas> several times. Since the file could be very long I am
> Thomas> searching for a better solution.
>
> Thomas> How can I replace several variables at once?
>
> The variable delimiters, eg, '$', were used inconsistently in the
> example you posted
Sorry, the example was typed into the MUA directly.
> def mysub(m):
> return vars[m.group(0)]
>
> # a literal dollar sign, followed by anything that isn't a dollar
> # sign followed by a literal dollar sign
> print re.sub('\$[^$]+\$', mysub, s)
Thank your for this example. That's was what I was looking for.
thomas
More information about the Python-list
mailing list