【发布时间】:2019-11-02 11:29:22
【问题描述】:
我尝试使用不匹配子字符串的正则表达式替换文本中的不同字符串。
我用:
v = {"Anna" : 'UNNK'}
text2 = "My name is Anna not Maria-Anna"
for i in v.keys():
w = r"\b{}(?![-|\w*])".format(i)
reg = re.compile(w)
text3 = reg.sub('UNK', text2)
print(text3)
代码返回:
“我的名字是 UNK 而不是 Maria-UNK”
我想返回的地方: “我叫 UNK,不是 Maria-Anna”
【问题讨论】: