删除Python中的数字(正则表达式)

删除Python中的数字(正则表达式),python,regex,digits,Python,Regex,Digits,我正在尝试删除字符串中的所有数字。 但是,下一个代码会删除任何单词中包含的数字。显然,我不想那样。 我一直在尝试许多正则表达式,但都没有成功 谢谢 结果: 这不能被删除,但结尾的数字是 在\d+之前添加一个空格 >>> s = "This must not b3 delet3d, but the number at the end yes 134411" >>> s = re.sub(" \d+", " ", s) >>> s 'This

我正在尝试删除字符串中的所有数字。 但是,下一个代码会删除任何单词中包含的数字。显然,我不想那样。 我一直在尝试许多正则表达式,但都没有成功

谢谢


结果:

这不能被删除,但结尾的数字是


在\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)

如果您的号码始终位于字符串末尾,请尝试: re.sub(“\d+$”,“”,s)

否则,你可以试试 re.sub(“(\s)\d+(\s)”,“\1\2”,s)

您可以调整后向引用以仅保留一个或两个空格(\s匹配任何白色分隔符)

尝试以下操作:

"\b\d+\b"

这将只匹配不属于另一个单词的数字。

要处理行首的数字字符串,请执行以下操作:

s = re.sub(r"(^|\W)\d+", "", s)

使用
\s
不是很好,因为它不处理制表符等。更好的解决方案的第一个切入点是:

re.sub(r"\b\d+\b", "", s)
请注意,该模式是一个原始字符串,因为
\b
通常是字符串的退格转义,我们希望使用特殊的单词边界正则表达式转义。一个稍微有点花哨的版本是:

re.sub(r"$\d+\W+|\b\d+\b|\W+\d+$", "", s)
当字符串的开头/结尾有数字时,尝试删除前导/尾随空格。我之所以说“尝试”,是因为如果末尾有多个数字,那么仍然有一些空格。

非正则表达式解决方案:

>>> s = "This must not b3 delet3d, but the number at the end yes 134411"
>>> " ".join([x for x in s.split(" ") if not x.isdigit()])
'This must not b3 delet3d, but the number at the end yes'
按“
”拆分,并通过DO检查块是否为数字,然后将它们重新连接在一起。更详细地说(不使用列表理解):


我不知道你的真实情况是什么,但大多数答案看起来不会处理负数或小数

re.sub(r“(\b\124;\ s+\-?\124^\-?)(\ d+\d*\。\d+)\b“,”)

上面也应该处理这样的事情,

“这不能是b3 delet3d,而是结尾的数字是-134.411”

但这仍然是不完整的-您可能需要一个更完整的定义,来定义您希望在需要解析的文件中找到的内容

编辑:还值得注意的是,“\b”会根据您使用的区域设置/字符集而变化,因此您需要稍微小心

>>>s = "This must not b3 delet3d, but the number at the end yes 134411"
>>>s = re.sub(r"\d*$", "", s)
>>>s
“这不能是b3 delet3d,而是结尾的数字是”

这将删除字符串末尾的数字。

您可以尝试此操作

s = "This must not b3 delet3d, but the number at the end yes 134411"
re.sub("(\s\d+)","",s) 
结果:

'This must not b3 delet3d, but the number at the end yes'
'This must not b3 delet3d, but the number at the end yes'
同样的规则也适用于

s = "This must not b3 delet3d, 4566 but the number at the end yes 134411" 
re.sub("(\s\d+)","",s) 
结果:

'This must not b3 delet3d, but the number at the end yes'
'This must not b3 delet3d, but the number at the end yes'

要仅匹配字符串中的纯整数,请执行以下操作:

\b(?<![0-9-])(\d+)(?![0-9-])\b
该页面上的所有其他8个正则表达式答案都会以各种方式因输入而失败

最后的冲刺以0-9领先。。。[0-9-] ... 保留-007,第二组中的破折号保留8-

或者\d代替0-9(如果您愿意)


它可以简化吗?

像“3at”这样的字符串怎么办?这里还有两个单元测试案例:“123应该被删除。”和“You have been 0wn3d”另一个是re.sub(“^\d+\s\s\d+\s\s\d+$”,“,“1 2 3对我来说失败”)\W可能比\s更好。另外,更好的变体应该是“\b\d+\b”,除非它对我不起作用!这不会删除的第一个或最后一个数字,s=s=“1234这不能是b3 delet3d,123而是结尾的数字是134411”我刚刚用您的字符串测试了它,得到了预期的结果\b匹配字符串的开头、结尾或任何非单词字符([a-Za-z0-9_])。我在IronPython中测试了它,不知道Python对单词boundaries的处理是否有问题我还没有尝试过,但是你能做一些类似的事情吗:[^\b]\d+[$\b]sharth:这基本上是一样的\b将在字符串的开头或结尾匹配。它是一个“空模式”,匹配一个单词和一个非单词。因此,re.sub(r'\b',!','1-2')给出了“!1!!2!”我的答案是唯一一个通用的答案,感谢您的支持,您可以通过我在答案中提供的链接(这是唯一一个带有图片rn的链接)看到所有其他链接的失败。可以删除\d+周围的paren,但可以用来捕获纯数字
max-3 cvd-19 agent-007 8-zoo 2ab c3d ef4 55g h66i jk77 
8m9n o0p2     million     0 22 333  4444