Regular Expression for Same Consecutive Numbers

喜欢而已 提交于 2021-02-05 06:50:23

问题


How do i write a regex to handle all instances where a the substring is 6 or more consecutive numbers such as this?

000000
111111
222222
333333
444444
555555

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

I plan to negative this afterwards so I can nullify strings with these cases.

Thanks in advance!


回答1:


To match strings only consisting of 6 or more identical digits, you may use

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

The pattern matches:

  • ^ - start of string
  • ([0-9]) - Capturing group 1 matching a digit
  • \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)
  • $ - end of string.


来源:https://stackoverflow.com/questions/42001668/regular-expression-for-same-consecutive-numbers

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!