简体   繁体   English

相同连续数的正则表达式

[英]Regular Expression for Same Consecutive Numbers

How do i write a regex to handle all instances where a the substring is 6 or more consecutive numbers such as this? 我如何编写正则表达式来处理子字符串是6个或更多连续数字的所有实例?

000000
111111
222222
333333
444444
555555

I tried [0-9]{6,} . 我试过[0-9]{6,}

I plan to negative this afterwards so I can nullify strings with these cases. 我计划在之后对此进行否定,因此我可以在这些情况下使字符串无效。

Thanks in advance! 提前致谢!

To match strings only consisting of 6 or more identical digits, you may use 要匹配仅包含6个或更多相同数字的字符串,您可以使用

^([0-9])\1{5,}$

The pattern matches: 模式匹配:

  • ^ - start of string ^ - 字符串的开头
  • ([0-9]) - Capturing group 1 matching a digit ([0-9]) - 捕获与数字匹配的组1
  • \\1{5,} - 5 or more occurrences (due to the limiting quantifier {5,} ) of the value captured into Group 1 (where \\1 is a backreference to Group 1 value) \\1{5,} - 5次或更多次出现(由于限制量词 {5,} )被捕获到第1组的值(其中\\1是对第1组值的反向引用
  • $ - end of string. $ - 结束字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM