I've been yanking clumps of hair out for 30 minutes doing this one...
I have a dictionary, like so:
{'search': 'replace',
'foo': 'bar'}
And a string like this:
Foo bar %foo% % search %.
I'd like to replace each variable with it's equivalent text from the dictionary:
Foo bar bar replace.
My current regex fails, so here it is (key
and value
are from dictionary.items()
):
re.sub(r'%\d+' + key + '[^%]\d+%', value, text)
Any help would be appreciated, as this regex stuff is driving me nuts...
If you're flexible with your syntax in your string, Python has a built in mechanism for that:
>>> print 'Hello, %(your_name)s, my name is %(my_name)s' % {'your_name': 'Blender', 'my_name': 'Ken'}
Hello, Blender, my name is Ken
Alternatively, if you want that syntax, I'd avoid regular expressions and just do this:
>>> vars = {'search': 'replace',
... 'foo': 'bar'}
>>> mystring = "Foo bar %foo% % search %."
>>> for k, v in vars.items():
... mystring = mystring.replace('%%%s%%' % k, v)
...
>>> print mystring
Foo bar bar % search %.
If you want it in one statement, you could do the following (assuming s is the string and d is the dictionary):
re.sub(r"[%]\s*[^%]+\s*[%]",lambda k:d.get(k[1,-1].strip(),k),s)
This uses a function in the replacement part to get each value from the dictionary, and ignores if it is not in the dictionary.
Edit: fixed bug with unwanted whitespace appearing in lookup key
Maybe I'm missing something, but wouldn't the following regex just work?
re.sub(r'%\s?' + key + '\s?%', value, text)
The only thing that's a bit special are the optional spaces; they can be matched with \s?
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With