python re sub dict_Python Regex Sub – 在替换中使用匹配作为Dict键

您可以将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'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值