在Python中使用Regex获得数字的平静

发布于 2025-01-17 22:13:08 字数 415 浏览 2 评论 0原文

我正在尝试使用 Python 中的正则表达式获取数字的一部分,我想收集 0(零)数字之后的数字序列,如下所示:

S_9900002127
S_9900000719
S_9900008012

因此,在上面的示例中我想获取:2127, 719和8012,我已经完成了正则表达式:

r'(_9(\d*)[.^0](\d*))' 

并得到了第二组正则表达式:[2],但结果是:2127、719和12..

请注意,第三个忽略数字 8,因为 0(零)。

有人可以帮我得到正确的结果2127、719和8012吗???

I´m trying to get a part of number using Regex in Python, I want to collect the sequence of numbers after 0 (zero) number, like this:

S_9900002127
S_9900000719
S_9900008012

So, in this example above I want to get: 2127, 719 and 8012, and I have done the Regex:

r'(_9(\d*)[.^0](\d*))' 

and get the second group of Regex: [2], but the result is: 2127, 719 and 12.

Look that the third one ignore the number 8 because the 0 (zero).

Could someone help me to get the correct result 2127, 719 and 8012 ???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赏烟花じ飞满天 2025-01-24 22:13:08

您可以使用

import re
text = r"""S_9900002127
S_9900000719
S_9900008012"""
print( re.findall(r'0+([1-9]\d*)

查看 Python 演示正则表达式演示详细信息

  • 0+ - 一个或多个 0
  • ([1-9]\d*) - 组1:一个非零数字,然后零个或多个数字
  • $ - 行尾。
, text, re.M) ) # => ['2127', '719', '8012']

查看 Python 演示正则表达式演示详细信息

  • 0+ - 一个或多个 0
  • ([1-9]\d*) - 组1:一个非零数字,然后零个或多个数字
  • $ - 行尾。

You can use

import re
text = r"""S_9900002127
S_9900000719
S_9900008012"""
print( re.findall(r'0+([1-9]\d*)

See the Python demo and the regex demo. Details:

  • 0+ - one or more 0s
  • ([1-9]\d*) - Group 1: a non-zero digit and then zero or more digits
  • $ - end of a line.
, text, re.M) ) # => ['2127', '719', '8012']

See the Python demo and the regex demo. Details:

  • 0+ - one or more 0s
  • ([1-9]\d*) - Group 1: a non-zero digit and then zero or more digits
  • $ - end of a line.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文