如何使用re.sub删除数字

时间:2019-01-28 04:35:43

标签: python

我正在尝试从字符串中删除数字,但请保留中间的数字。现在,我对以下代码的输出是:

  

“这是一个单词开头或结尾的测试编号,不应删除中间的一个。”

但我希望它是

  

“这是一个单词开头或结尾的测试编号,但不能删除midd3le中的一个。”

我应该怎么做?

String = "This is a test numbers33 at end or 333beginning of a word should be deleted but not ones in the midd3le."

s = re.sub("[\d]", "", String)

1 个答案:

答案 0 :(得分:0)

您可以尝试以下模式:

String = "This is a test numbers33 at end or 333beginning of a word should be deleted but not ones in the midd3le."

s = re.sub(r"\b\d+|\d+\b", "", String)

Demo

相关问题