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 i.e. 111111
or 999999
.
Is there a way to use a negative look-around or something with this regex?
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 i.e. 111111
or 999999
.
Is there a way to use a negative look-around or something with this regex?
Share Improve this question edited Jun 10, 2015 at 22:56 Wiktor Stribiżew 628k41 gold badges498 silver badges616 bronze badges asked Jun 10, 2015 at 20:36 HaggardWolfHaggardWolf 235 bronze badges 3- Is a string with 7 repeating numbers OK? Also your regex doesn't make sense. Why is 1 escaped? Are you just that isn't a regex used in a substitution regex? – Martin Konecny Commented Jun 10, 2015 at 20:42
- What is the puting language or tool you are using? – Wiktor Stribiżew Commented Jun 10, 2015 at 21:39
- To be clear, I am using a jQuery plugin called validation engine. I am trying to add a custom regex to the language file. I am just starting to dig into regex and am very novice. The regex I pasted above was something I found online that seems to work on regexr. to find repeating numbers. And yeah, the user can only input 6 numbers so 7 repeating numbers is ok. – HaggardWolf Commented Jun 10, 2015 at 22:25
2 Answers
Reset to default 6You can use this 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.
I'd rather go with the \d
shorthand class for digits since \w
also allows letters and an underscore.
^(?!.*(\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}
)..*
- Match 0 or more characters up to the$
- End of string/line.
See demo. This regex will allow
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745234852a4617842.html
评论列表(0条)