快速搜索和替换正则表达式

时间:2016-02-19 12:23:33

标签: python regex

尝试使用re

快速搜索并替换Python中的字符串
replacements = {r'\btest\b': 'Exam', r'\bhello\b': 'Hi', r'\bruby\b': 'Gem'}
replacements_pattern = '|'.join(replacements.keys())
text = 'this is a ruby Test, testing'
re.sub(replacements_pattern, lambda match: replacements[match.group()], text, re.IGNORECASE)

text
>> 'this is a Gem Exam, testing'

问题在于,match.group()会回复“测试”,因此无法与密钥r'\btest\b'匹配,所以我该如何处理?

编辑:您可以提取断点,st:

replacements = {r'test': 'Exam', r'hello': 'Hi', r'ruby': 'Gem'}
replacements_pattern = r'(?i)\b(?:' + '|'.join(replacements) + r')\b'
replacements_pattern = re.compile(replacements_pattern)
text = 'this is a ruby Test, testing'
re.sub(replacements_pattern, lambda match: replacements[match.group().lower()], text, re.IGNORECASE)

感谢您的建议到目前为止!

剩下的问题:在match.group()中检索到的密钥可能与字典中的密钥不匹配,从而导致匹配错误!

1 个答案:

答案 0 :(得分:1)

您不需要在dict键中提供\b。只需更改下面的模式,

replacements = {r'test': 'Exam', r'hello': 'Hi', r'ruby': 'Gem'}
replacements_pattern = r'\b(?:' + '|'.join(replacements) + r')\b'

进行不区分大小写的匹配,

replacements_pattern = r'(?i)\b(?:' + '|'.join(replacements) + r')\b'