Blender
Blender

Reputation: 298146

Regex to replace %variables%

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...

Upvotes: 7

Views: 7763

Answers (4)

murgatroid99
murgatroid99

Reputation: 20267

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

Upvotes: 4

Pi Delport
Pi Delport

Reputation: 10598

Using the replacement function support of re.sub:

def replace(s, kw, pattern=re.compile(r'%\s*(\w+)\s*%')):
    """
    Replace delimited keys in a string with values from kw.

    Any pattern may be used, as long as the first group defines the lookup key.
    """
    lookup = lambda match: kw.get(match.group(1), match.group())
    return pattern.sub(lookup, s)

>>> replace('Foo bar %foo% % search %.', {'search': 'replace', 'foo': 'bar'})
'Foo bar bar replace.'

You can change the lookup function to customize how lookup errors are treated, and so on.

Upvotes: 2

alienhard
alienhard

Reputation: 14692

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?.

Upvotes: 2

Ken Kinder
Ken Kinder

Reputation: 13140

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 %.

Upvotes: 8

Related Questions