mabounassif
mabounassif

Reputation: 2341

python regex for string replacement

I want to replace parts of a string that contains the following words "$%word$%" I want to replace it with the value of a dictionary with the corresponding key equal to word.

In other words if I have a string: "blahblahblah $%word$% blablablabla $%car$%" and a dictionary {word:'wassup', car:'toyota'}

The string would be "blahblahblah wassup blablablabla toyota"

How can you implement it in python, I was thinking about using string replacement and regex.

Upvotes: 1

Views: 1145

Answers (3)

Ferdinand Beyer
Ferdinand Beyer

Reputation: 67247

Use re.sub with a function as the repl parameter:

import re

text =  "blahblahblah $%word$% blablablabla $%car$%"
words = dict(word="wassup", car="toyota")

def replacement(match):
    try:
        return words[match.group(1)]  # Lookup replacement string
    except KeyError:
        return match.group(0)  # Return pattern unchanged

pattern = re.compile(r'\$%(\w+)\$%')
result = pattern.sub(replacement, text)

If you want to pass the replacement table at the time you use re.sub, use functools.partial:

import functools

def replacement(table, match):
    try:
        return table[match.group(1)]
    except:
        return match.group(0)

table = dict(...)
result = pattern.sub(functools.partial(replacement, table), text)

...or a class implementing __call__:

class Replacement(object):
    def __init__(self, table):
        self.table = table
    def __call__(self, match):
        try:
            return self.table[match.group(1)]
        except:
            return match.group(0)

 result = pattern.sub(Replacement(table), text)

Upvotes: 8

eyquem
eyquem

Reputation: 27585

import re

text =  "blahblahblah $%word$% blablablabla $%car$%"
words = dict(word="wassup", car="toyota")

regx = re.compile('(\$%%(%s)\$%%)' % '|'.join(words.iterkeys()))

print regx.sub(lambda mat: words[mat.group(2)], text)

result

blahblahblah wassup blablablabla toyota

Upvotes: 1

andronikus
andronikus

Reputation: 4220

The re module is the one you want.

You might want to reconsider your choice of delimiters, though. $% might get problematic because $ is a reserved character in regex. Up to you though, just remember to use '\\$' or r'\$' (that's a raw string. Very useful if you're doing regex stuff in python.) in your patterns.

Upvotes: 0

Related Questions