【问题标题】:Regex PCRE: validate string that does not contains 3 or more consecutive digits正则表达式 PCRE:验证不包含 3 个或更多连续数字的字符串
【发布时间】:2010-10-18 13:09:23
【问题描述】:

我已经搜索了这些问题,但找不到答案。我需要一个模式,与 php preg_match 函数一起使用,只匹配不包含 3 个或更多连续数字的字符串,例如:

rrfefzef        => TRUE
rrfef12ze1      => TRUE
rrfef1zef1      => TRUE
rrf12efzef231   => FALSE
rrf2341efzef231 => FALSE

到目前为止,我已经编写了以下正则表达式:

@^\D*(\d{0,2})?\D*$@

它只匹配只出现一次\d{0,2}的字符串

如果其他人有时间帮助我,我将不胜感激:)

问候,

【问题讨论】:

  • 你的第二个例子是假的,不是吗?
  • @Gábor Lipták:他只想检查连续数字,以便正确评估。
  • 你真的不能通过搜索/\d{2,}/ 并否定结果来简化这个过程吗?
  • @poke - 12 对我来说似乎是连续的数字......
  • @poke 我可以看到rrfef12ze1@Gábor 中的两个连续数字

标签: php regex pcre


【解决方案1】:

如果字符串有两个或多个连续数字,则拒绝该字符串:\d{2,}

或仅在没有连续数字时使用负前瞻匹配:^(?!.*\d{2}).*$

【讨论】:

    【解决方案2】:
    /^(.(?!\d\d\d))+$/
    

    匹配所有后面不跟三位数字的字符。 Example.

    【讨论】:

      【解决方案3】:

      您可以搜索\d\d,它将匹配所有错误字符串。然后您可以调整您的进一步程序逻辑以正确响应。

      如果您确实需要对包含相邻数字的字符串进行“肯定”匹配,这也应该有效:

      ^\D?(\d?\D)*$
      

      【讨论】:

        【解决方案4】:

        有什么阻止您简单地在 preg_match() 函数前加上“!”,从而反转布尔结果?

        !preg_match( '/\d{2,}/' , $subject );
        

        简单多了...

        【讨论】:

        • 取决于我们是考虑他的测试用例还是他的书面规范 - 它满足“...不包含 2 个或更多连续数字”,但测试用例似乎是3个或更多。然后,再次查看测试用例,他的意思可能是 3 个或更多 连续 位(即“123” = True,但“134 " = 假)。
        • 事实上我正在使用一个框架,在这种情况下,封装了 preg_match 返回,所以我不能只是反转 preg_match 结果。我本可以使用回调,但我更喜欢正则表达式方法;)
        【解决方案5】:

        如果我正确解释您的要求,则以下正则表达式匹配您的有效输入而不匹配无效输入。

        ^\D*\d*\D*\d?(?!\d+)$
        

        解释如下

        > # ^\D*\d*\D*\d?(?!\d+)$
        > # 
        > # Options: case insensitive; ^ and $ match at line breaks
        > # 
        > # Assert position at the beginning of a line (at beginning of the string or
        > after a line break character) «^»
        > # Match a single character that is not a digit 0..9 «\D*»
        > #    Between zero and unlimited times, as many times as possible, giving back
        > as needed (greedy) «*»
        > # Match a single digit 0..9 «\d*»
        > #    Between zero and unlimited times, as many times as possible, giving back
        > as needed (greedy) «*»
        > # Match a single character that is not a digit 0..9 «\D*»
        > #    Between zero and unlimited times, as many times as possible, giving back
        > as needed (greedy) «*»
        > # Match a single digit 0..9 «\d?»
        > #    Between zero and one times, as many times as possible, giving back as
        > needed (greedy) «?»
        > # Assert that it is impossible to match the regex below starting at this
        > position (negative lookahead)
        > «(?!\d+)»
        > #    Match a single digit 0..9 «\d+»
        > #       Between one and unlimited times, as many times as possible,
        > giving back as needed (greedy) «+»
        > # Assert position at the end of a line (at the end of the string or before a
        > line break character) «$»
        

        【讨论】:

          猜你喜欢
          • 2013-09-24
          • 2018-07-25
          • 2012-02-11
          • 2011-11-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多