Python 中的多个特定正则表达式替换 [英] Multiple, specific, regex substitutions in Python
问题描述
我想做的是在给定的文本中进行特定的替换.例如,<"应该改为'[','>' 到']',等等.它类似于此处给出的解决方案:如何在 python 中使用正则表达式进行多次替换?,即
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)
现在,问题是我还想替换正则表达式匹配的模式.例如,我想将 'fo.+' 替换为 'foo',将 'ba[rz]*' 替换为 'bar'.
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'.
删除地图(代码中的 re.escape 有帮助,以便正则表达式实际匹配,但随后我收到关键错误,因为例如,'barzzzzzz' 将是匹配项,并且我想替换某些内容,但是'barzzzzzz' 不是字典中的键,文字字符串 'ba[rz]*' 是.我该如何修改这个函数才能工作?
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?
(在不相关的注释中,这些 'foo' 和 'bar' 的东西从何而来?)
(On an unrelated note, where do these 'foo' and 'bar' things come from?)
推荐答案
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)
给予:
bar [
这篇关于Python 中的多个特定正则表达式替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!