问题
What I would like to do is to make specific substitions in a given text. For example, '<' should be changed to '[', '>' to ']', and so forth. It is similar to the solution given here: How can I do multiple substitutions using regex in python?, which is
import re
def multiple_replace(dict, text):
# Create a regular expression from the dictionary keys
regex = re.compile("(%s)" % "|".join(map(re.escape, dict.keys())))
# For each match, look-up corresponding value in dictionary
return regex.sub(lambda mo: dict[mo.string[mo.start():mo.end()]], text)
Now, the problem is that I would also like to replace regex-matched patterns. For example, I want to replace 'fo.+' with 'foo' and 'ba[rz]*' with 'bar'.
Removing the map(re.escape in the code helps, so that the regex actually matches, but I then receive key errors, because, for example, 'barzzzzzz' would be a match, and something I want to replace, but 'barzzzzzz' isn't a key in the dictionary, the literal string 'ba[rz]*' is. How can I modify this function to work?
(On an unrelated note, where do these 'foo' and 'bar' things come from?)
回答1:
import re
def multiple_replace(dict, text):
# Create a regular expression from the dictionary keys
regex = re.compile(r'(%s)' % "|".join(dict.keys()))
return regex.sub(lambda mo: dict[
[ k for k in dict if
re.search(k, mo.string[mo.start():mo.end()])
][0]], text)
d = { r'ba[rz]*' : 'bar', '<' : '[' }
s = 'barzzzzzz <'
print multiple_replace(d, s)
Gives:
bar [
回答2:
Just do multiple sub
calls.
On an unrelated note, Jargon File to the rescue: Metasyntactic variables, foo.
来源:https://stackoverflow.com/questions/17584100/multiple-specific-regex-substitutions-in-python