您可以将callable传递给re.sub,告诉它如何处理匹配对象.
s = re.sub(r'', lambda m: replacement_dict.get(m.group()), s)
如果所述单词不在替换词典中,则使用dict.get允许您提供“后备”
lambda m: replacement_dict.get(m.group(), m.group())
# fallback to just leaving the word there if we don't have a replacement
我会注意到当使用re.sub(和family,即re.split)时,在指定你想要的替换周围存在的东西时,使用外观表达式通常更清晰,这样你的匹配周围的东西就不会被淘汰.所以在这种情况下我会写你的正则表达式
r'(?<=)'
否则你必须在lambda中的括号中进行拼接.为了清楚我在说什么,一个例子:
s = "this is stuffthis is other stuff"
d = {'othertag': 'blah'}
#this doesn't work because `group` returns the whole match, including non-groups
re.sub(r'', lambda m: d.get(m.group(), m.group()), s)
Out[23]: 'this is stuffthis is other stuff'
#this output isn't exactly ideal...
re.sub(r'', lambda m: d.get(m.group(1), m.group(1)), s)
Out[24]: 'sometagthis is stuffblahthis is other stuffclosetag'
#this works, but is ugly and hard to maintain
re.sub(r'', lambda m: ''.format(d.get(m.group(1), m.group(1))), s)
Out[26]: 'this is stuffthis is other stuff'
#lookbehind/lookahead makes this nicer.
re.sub(r'(?<=)', lambda m: d.get(m.group(), m.group()), s)
Out[27]: 'this is stuffthis is other stuff'