【问题标题】:Check whether text contains x numbers in a row [closed]检查文本是否连续包含x个数字[关闭]
【发布时间】:2015-12-29 14:58:13
【问题描述】:

是否有一个选项可以检查给定的文本是否连续包含 x 个数字?

例子:

da$ c0220 -> True
dsad458d69 -> False

我想我应该使用regular expressions,但我不知道如何使用。

【问题讨论】:

  • 正好是 x 个数字,还是最少 x 个数字?
  • \d{n} 其中n 是确切数量,\d{n,m} 其中n 是最小值,m 是允许的最大值。
  • ...和\d{n,} 至少 n个数字
  • 我认为这个问题不是很清楚,必须关闭。为这两种可能的情况提供正则表达式非常容易。投票重新开放。
  • @hwnd \d{3} 将匹配123 中的1234

标签: python regex parsing


【解决方案1】:

下面的正则表达式检查 10 个连续的数字

\d{10}

在 Python 中,这变成了

if re.search(r"\d{10}", subject):
    # Successful match
else:
    # Match attempt failed

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-15
    • 1970-01-01
    • 2015-07-31
    相关资源
    最近更新 更多