Replace text with value form dictionary (regexp)

Jean-Pierre Bergamin james at
Wed Mar 3 15:09:19 CET 2004

Hi there

I want to implement a small templating system where values in angle brackets
should be replaced by the corresponding dicitionary value ("foo {x} bar" ->
"foo 10 bar");

>>> d = {}
>>> d["x"] = 10
>>> p = re.compile('{ ( [^}]* ) }', re.VERBOSE)
>>> p.sub(r'd["\1"]','foo {x} bar')
'foo d["x"] bar'

How can I now put the actual value of d["x"] into the string 'foo d["x"]

Thanks for your help.



More information about the Python-list mailing list