简体   繁体   English

正则表达式不匹配6个重复的数字

[英]Regex not matching 6 repeated numbers

I am trying to get a regular expression to work but am stumped. 我正试图使一个正则表达式起作用,但很沮丧。 What I want is to do the inverse of this: 我想要做的是与此相反:

/(\w)\1{5,}/

This regex does the exact opposite of what I'm trying to do. 此正则表达式与我要执行的操作完全相反。 I would like to get everything but a string that has 6 repeating numbers ie 111111 or 999999 . 我想得到除具有6个重复数字的字符串(即111111999999

Is there a way to use a negative look-around or something with this regex? 有没有办法在此正则表达式中使用否定的环顾四周?

You can use this rgex: 您可以使用以下rgex:

/^(?!.*?(\w)\1{5}).*$/gm

RegEx Demo 正则演示

(?!.*?(\\w)\\1{5}) is a negative lookaahead that will fail the match if there are 6 consecutive same word characters in it. (?!.*?(\\w)\\1{5})为负前瞻,如果其中有6个连续的相同单词字符,则匹配失败。

I'd rather go with the \\d shorthand class for digits since \\w also allows letters and an underscore. 我宁愿与去\\d 速记班 ,因为对数字\\w也允许字母和下划线。

^(?!.*(\d)\1{5}).*$

Regex explanation: 正则表达式说明:

  • ^ - Start of string/line anchor ^ -字符串/行锚的开始
  • (?!.*(\\d)\\1{5}) - The negative lookahead checking if after an optional number of characters ( .* ) we have a digit ( (\\d) ) that is immediately followed with 5 identical digits ( \\1{5} ). (?!.*(\\d)\\1{5}) -否定超前检查是否在可选数目的字符( .* )之后是否有数字( (\\d) ),然后紧跟5个相同的数字(( \\1{5} )。
  • .* - Match 0 or more characters up to the .* -最多匹配0个或更多字符
  • $ - End of string/line. $ -字符串/行的结尾。

See demo . 参见演示 This regex will allow 此正则表达式将允许

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

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