相同连续数字的正则表达式 [英] Regular Expression for Same Consecutive Numbers

查看:62
本文介绍了相同连续数字的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何编写正则表达式来处理子字符串是 6 个或更多连续数字的所有实例,例如这样?

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

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

我打算事后否定这个,这样我就可以在这些情况下使字符串无效.

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

提前致谢!

推荐答案

要匹配仅包含 6 个或更多相同数字的字符串,您可以使用

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

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

模式匹配:

  • ^ - 字符串的开始
  • ([0-9]) - 捕获匹配数字的组 1
  • \1{5,} - 出现 5 次或更多(由于 限制量词 {5,}) 捕获到组 1 中的值(其中 \1 是一个反向引用到第 1 组值)
  • $ - 字符串结束.
  • ^ - 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.

这篇关于相同连续数字的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆