在正则表达式中使用 sub() 方法时如何避免突变?


namesRegex = re.compile(r"Agent w+") 
namesRegex.sub('CENSORED', 'Agent Alice gave the secret documents to Agent Bob.')

当我这样做时,它不仅会改变代理,还会改变爱丽丝和鲍勃。我的意思是它多改了一个词。 我试图理解这一点,例如,当我只想改变爱丽丝时,它也改变了"给"。 我怎么只能更改正则表达式中的一个单词?

还有一个问题,我们像这样写 re.compile(r".* 等( 但即使我们不写"r",比如 r.compile(".* 等"(,它也会做同样的事情。那我们为什么要在那边写r信呢?

您可以改为'Agent '正后看模式的一部分,以便re.sub仅与代理的名称匹配,因此仅将代理的名称替换为'CENSORED'

namesRegex = re.compile(r"(?<=Agent )w+") 

查看正则表达式101

您可以针对不同的输入测试正则表达式,并查看匹配的内容。它甚至解释了比赛中使用了哪些规则。

例如,对于Agent w+,解释是:

Agent matches the characters Agent literally (case sensitive)
w+
matches any word character (equal to [a-zA-Z0-9_])
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed

相关内容

最新更新