1

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.

mabounassif
  • 2,311
  • 6
  • 29
  • 46

3 Answers3

8

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)
Ferdinand Beyer
  • 64,979
  • 15
  • 154
  • 145
  • What if the dictionary is created in another method? How would I implement replacement? I can't add arguments to replacement. – mabounassif Aug 24 '11 at 22:02
  • Very similar to this question; http://stackoverflow.com/questions/7182546/how-to-replace-the-nth-appearance-of-a-needle-in-a-haystack-python – Matt Warren Aug 24 '11 at 22:04
  • @mabounassif - Let `replacement` take the dictionary as a parameter, then use `functools.partial()` to create a one-argument wrapper function that passes the dictionary. I'll update my answer to give an example. – Ferdinand Beyer Aug 25 '11 at 05:53
1
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
eyquem
  • 26,771
  • 7
  • 38
  • 46
0

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.

andronikus
  • 4,125
  • 2
  • 29
  • 46