Regular Expression to Match 3 or More Consecutive Sequential Characters and Consecutive Identical Characters

Regex to detect consecutive characters

This regex should work:

^(?!.*?[ '.-]{2})[A-Za-z0-9 '.-]{1,30}$

Working Demo: http://regex101.com/r/pJ3hJ9

RegEx No more than 2 identical consecutive characters and a-Z and 0-9

http://regexr.com?34vo9

The regular expression:

^(?=.{8,20}$)(([a-z0-9])\2?(?!\2))+$

The first lookahead ((?=.{8,20}$)) checks the length of your string. The second portion does your double character and validity checking by:

(
([a-z0-9]) Matching a character and storing it in a back reference.
\2? Optionally match one more EXACT COPY of that character.
(?!\2) Make sure the upcoming character is NOT the same character.
)+ Do this ad nauseum.
$ End of string.

Okay. I see you've added some additional requirements. My basic forumla still works, but we have to give you more of a step by step approach. SO:

^...$

Your whole regular expression will be dropped into start and end characters, for obvious reasons.

(?=.{n,m}$)

Length checking. Put this at the beginning of your regular expression with n as your minimum length and m as your maximum length.

(?=(?:[^REQ]*[REQ]){n,m})

Required characters. Place this at the beginning of your regular expression with REQ as your required character to require N to M of your character. YOu may drop the (?: ..){n,m} to require just one of that character.

(?:([VALID])\1?(?!\1))+

The rest of your expression. Replace VALID with your valid Characters. So, your Password Regex is:

^(?=.{8,20}$)(?=[^A-Za-z]*[A-Za-z])(?=[^0-9]*[0-9])(?:([\w\d*?!:;])\1?(?!\1))+$

'Splained:

^
(?=.{8,20}$) 8 to 20 characters
(?=[^A-Za-z]*[A-Za-z]) At least one Alpha
(?=[^0-9]*[0-9]) At least one Numeric
(?:([\w\d*?!:;])\1?(?!\1))+ Valid Characters, not repeated thrice.
$

http://regexr.com?34vol Here's the new one in action.

RegEx to find two or more consecutive chars

This should do the trick:

[a-zA-Z]{2,}

RegEx in JS to find No 3 Identical consecutive characters

This depends on what you actually mean. If you only want to match three non-identical characters (that is, if abb is valid for you), you can use this negative lookahead:

(?!(.)\1\1).{3}

It first asserts, that the current position is not followed by three times the same character. Then it matches those three characters.

If you really want to match 3 different characters (only stuff like abc), it gets a bit more complicated. Use these two negative lookaheads instead:

(.)(?!\1)(.)(?!\1|\2).

First match one character. Then we assert, the this is not followed by the same character. If so, we match another character. Then we assert that these are followed neither by the first nor the second character. Then we match a third character.

Note that those negative lookaheads ((?!...)) do not consume any characters. That is why they are called lookaheads. They just check what is coming next (or in this case what is not coming next) and then the regex continues from where it left of. Here is a good tutorial.

Note also that this matches anything but line breaks, or really anything if you use the DOTALL or SINGLELINE option. Since you are using JavaScript you can just activate the option by appending s after the regexes closing delimiter. If (for some reason) you don't want to use this option, replace the .s by [\s\S] (this always matches any character).

Update:

After clarification in the comments, I realised that you do not want to find three non-identical characters, but instead you want to assert that your string does not contain three identical (and consecutive) characters.

This is a bit easier, and closer to your former question, since it only requires one negative lookahead. What we do is this: we search the string from the beginning for three consecutive identical characters. But since we want to assert that these do not exist we wrap this in a negative lookahead:

^(?!.*(.)\1\1)

The lookahead is anchored to the beginning of the string, so this is the only place where we will look. The pattern in the lookahead then tries to find three identical characters from any position in the string (because of the .*; the identical characters are matched in the same way as in your previous question). If the pattern finds these, the negative lookahead will thus fail, and so the string will be invalid. If not three identical characters can be found, the inner pattern will never match, so the negative lookahead will succeed.

Check identical consecutive groups with regular expressions

Use

^(?:\d+ )*?((\d{2,3})(?: \2){1,3})$

See proof. It will match any digit/space groups at the start of the string, and then caputure repeating chunks of two or three digits separated with a single space. \s will match any whitespace and \s+ will match any one or more whitespace characters, replace the space in the pattern if you wish to have any whitespace support.

Regular Expression to not allow 3 consecutive characters

I solved by using trial and error:

Regex pattern = new Regex(@"^(?!.*(.)\1\1)(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]{8,20}$");

Regex to match two consecutive characters unless followed/preceded by more of the same character

You may use a lookaround based regex:

String text = "qqq Eqq Eqqq Cqq Eqq Fq";
text = text.replaceAll("(?<!q)q{2}", "h");
System.out.println(text);
// => hq Eh Ehq Ch Eh Fq

See the Java demo and a regex demo.

Details

  • (?<!q) - a negative lookbehind that fails the match if there is a q immediately to the left of the current location
  • q{2} - 2 q chars.

Note: if you plan to only replace 2 q chars not surrounded with qs, add a negative lookahead (?!q) at the end, "(?<!q)q{2}(?!q)".

Regex for not containing consecutive characters

You could use multiple lookaheads (neg. ones in this case):

^(?!.*__)(?!.*_$)[A-Za-z]\w*$

See a demo on regex101.com.


Broken down this says:

^           # start of the line
(?!.*__) # neg. lookahead, no two consecutive underscores (edit 5/31/20: removed extra Kleene star)
(?!.*_$) # not an underscore right at the end
[A-Za-z]\w* # letter, followed by 0+ alphanumeric characters
$ # the end


As JavaScript snippet: