避免相等,连续数字的正则表达式

时间:2021-03-05 20:32:15

标签: regex

我正在尝试针对以下标准进行构建

     The following captures should not be allowed:

       1) Null fields
       2) Field captured as “000000”
       3) Field captured with consecutive numbers “123456”
       4) Field captured with equal numbers  “111111”
       5) input length is 7 digits not less or more 
<块引用>

使用的正则表达式 = ^(?!([0-9]{7})).$

Examples:- 

1111111 1234567 456789 0000000 we invalid successfully 

VALID like 8563241 5342861 all the valid scenarios are also not working .
Could any one help me how to capture valid scenarios with a regular expression .

谢谢, 普拉萨德

1 个答案:

答案 0 :(得分:1)

经过一番聊天,我们完成了以下规则,其中输入:

  • 必须正好是 7 位数字。
  • 不能包含任何三个相同的重复字符,例如:“111”。
  • 不能包含任何三个连续的数字(正向/反向),例如:“123”或“321”。

因此,以下内容似乎确实符合您的要求:

^(?!.*?(?:012|123|234|345|456|567|678|789|987|876|765|654|543|432|321|210|(\d)\1\1))\d{7}$

查看在线demo

  • ^ - 起始字符串锚点。
  • (?! - 打开否定前瞻:
    • .*? - 0+ 个字符(懒惰)。
    • (?: - 打开非捕获组:
      • 012|123|234|345|456|567|678|789|987|876|765|654|543|432|321|210 - 在您的输入中避免任何地方的所有替代方法。
    • | - 或:
    • (\d)\1\1 - 使用对同一组的两个反向引用捕获的单个数字。
    • )) - 关闭非捕获组和负前瞻。
  • \d{7} - 7 位数字。
  • $ - 结束字符串锚点。