例如,我想使用这种模式
rules = {
'\s': '_',
'.(?P<word>\w)': '\1',
'text1': 'text2',
#etc
}
使用 re.sub()
有一些例子像this , 但它不适用于正则表达式特殊字符。
最佳答案
我在 python 中使用正则表达式时使用原始字符串。使您不必转义特殊字符:https://docs.python.org/2/library/re.html
尝试:
rules = {
r"\s": r"_",
r"text1": r"text2",
#etc
}
关于python - 基于 Python 中的几个正则表达式规则进行替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36058762/