我正在尝试删除字符串中的所有数字。
但是,下一个代码也会删除任何单词中包含的数字。显然,我不想要那样。
我一直在尝试许多正则表达式,但都没有成功。
谢谢!
s = "This must not be deleted, but the number at the end yes 134411"
s = re.sub("\d+", "", s)
print s
结果:
最佳答案
在\ d +之前添加一个空格。
>>> s = "This must not b3 delet3d, but the number at the end yes 134411"
>>> s = re.sub(" \d+", " ", s)
>>> s
'This must not b3 delet3d, but the number at the end yes '
编辑:查看评论后,我决定形成一个更完整的答案。我认为这是所有情况的原因。
s = re.sub("^\d+\s|\s\d+\s|\s\d+$", " ", s)