Skip to content Skip to sidebar Skip to footer

Regex For No More Than 2 Consecutive Numbers No More Than 2 Repeated Characters?

I am looking to create a regex with conditions: Minimum length 6 At least one number and one letter must be used No more than 2 consecutive numbers (like 123) No more than 2 repea

Solution 1:

Try this Regex:

^(?=[\D]*\d)(?=[^a-zA-Z]*[a-zA-Z])(?=.{6,})(?!.*(\d)\1{2})(?!.*([a-zA-Z])(?:.*?\2){2,}).*$

Demo

Explanation:

  • ^ - start of the string
  • (?=[\D]*\d) - positive lookahead - checks for the presence of a digit
  • (?=[^a-zA-Z]*[a-zA-Z]) - positive lookahead - checks for the presence of a letter
  • (?=.{6,}) - positive lookahead - checks for the presence of atleast 6 literals
  • (?!.*(\d)\1{2}) - Negative lookahead - Checks for the ABSENCE of 3 consecutive digits. It will allow 2 consecutive digits though. If you do not want even 2 consecutive digits, then remove {2} from this part
  • (?!.*([a-zA-Z])(?:.*?\2){2,}) - Negative lookahead - validates that no letter should be present more than 2 times in the string
  • .* - capture the string
  • $ - end of the string

OUTPUT:

jj112233         -Matches as it has atleast one letter, digit. Not more than 2 consecutive digits/letter. Has atleast 6 characters
jkhsfsndbf8uwwe  -matches 
a1234            -does not matchas length<6 
nsds312          -matches
111aaa222        -does not matchas it has more than 2 consecutive digits and also more than 2 repeated letters 
aa11bbsd         -match
hgshsadh12       -does not matchas it has more than 2 `h`
hh8uqweuu        -does not matchas it has more than 2 `u`

Post a Comment for "Regex For No More Than 2 Consecutive Numbers No More Than 2 Repeated Characters?"