简体   繁体   中英

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM