Drew Madelung
Drew Madelung

Reputation: 1

Regex to find 4+ repeating lowercase letters and requiring 2+ numbers, special chars, and uppercase

I am putting together a regex for strong passwords and having issues figuring out how to bring the requirements together. They are:

I started with a library entry from regex101 to find the 2 or more characters but I can't figure out how to identify repeating lowercase that don't require to be the same lowercase character, the digit/capital/special requirement is not staying within the character group, and it is not letting it start with a special character.

For the repeating lowercase I thought doing a negative lookahead or lookbehind like either of these would work but it is not:

(?!.*[a-z]{4,})
(?<![a-z])[a-z]{4,}(?![a-z])

I cannot see where within the full regex the issue of starting with a special or checking beyond the capturing group is occurring.

Here is what I have so far for the full regex without the repeating lowercase requirement included:

\b(?=(?:.*[A-Z]){2,})(?=(?:.*[a-z]){2,})(?=(?:.*\d){2,})(?=(?:.*[!@#$%^&*()\-_=+{};:,.>\]\[]){2,})([A-Za-z0-9!#$@%^&*()\-_=+{};:,<.>\]\[]{15})(?!\S)

Link to https://regex101.com/r/E418tG/1

Upvotes: 0

Views: 173

Answers (1)

sln
sln

Reputation: 2759

To keep from overrunning the 15 character boundary, the (?<!\S)\S{15}(?!\S) should be enough regulation at the beginning which throttles the other terms well enough.

(?<!\S)(?=\S{15}(?!\S))(?!\S*[a-z]{3})(?=\S*[A-Z]\S*[A-Z])(?=\S*[0-9]\S*[0-9])(?=\S*[\\!-/:-@\[\]-`{-~]\S*[\\!-/:-@\[\]-`{-~])\S*(?!\S)

https://regex101.com/r/bZTIQI/1

 (?<! \S )
 (?=                           # 15 characters exactly
    \S{15} 
    (?! \S )
 )
 (?! \S* [a-z]{3} )            # Not more than 3 lowercase letters in a row
 (?= \S* [A-Z] \S* [A-Z] )     # 2 or more capital letters
 (?= \S* [0-9] \S* [0-9] )     # 2 or more digits
 (?=                           # 2 or more special characters
    \S* [\\!-/:-@\[\]-`{-~] 
    \S* [\\!-/:-@\[\]-`{-~] 
 )
 \S* 
 (?! \S )

The many lower case in a row is up to you, I put 3 in a row as not allowed.
Don't need to check for more with these conditions, minimum allowed (not allowed) is sufficient.

Upvotes: 1

Related Questions