尝试使用re
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()中检索到的密钥可能与字典中的密钥不匹配,从而导致匹配错误!
答案 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'