我想知道如何替换一个确切的单词但忽略标点符号
我看到这个answer这很好但是对我来说是失败的。
以下是我想要替换的示例:
"hi my name is John, and I am 30 yrs old."
到
"to hi my name is ted, and I am 30 yrs old."
由于
答案 0 :(得分:2)
>>> s = "hi my name is John, and I am 30 yrs old."
>>> new_s = s.replace("John", "Ted")
>>> new_s
'hi my name is Ted, and I am 30 yrs old.'
答案 1 :(得分:1)
\bJohn\b
这应该为你做。按ted
替换。