re.sub不修改python中的字符串

时间:2016-08-11 15:37:32

标签: python

我有一个小的python程序,我希望单词“已验证”(无论是用大写,小写还是大写和小写混合编写)。要重置为“已验证”。我如何重写下面的代码?

 import re
 text="verified, vERIFIED, VERIFIED"
 text=re.sub(r'\verified', 'Verified', text, flags=re.IGNORECASE)
 print text

Expected output: Verified, Verified, Verified

Actual output:verified, vERIFIED, VERIFIED

1 个答案:

答案 0 :(得分:2)

只需在v

之前删除反斜杠即可
import re
text="verified, vERIFIED, VERIFIED"
text=re.sub(r'verified', 'Verified', text, flags=re.IGNORECASE)
print text