I have a requirement for password with below requirements, out of which all fulfilled, except repeated words/patterns.
This is my Regex which fulfills everything but last (repeating pattern) requirement.
(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[.,!@#$%^+=_])(?!.*(.)\1).{8,20}$
Is there a way I can tackle it within above Regex?
Assuming your password should not match spaces, you can use \S
to match non whitespace characters instead of using a dot.
^(?=[^\d\s]*\d)(?=[^\sa-z]*[a-z])(?=[^\sA-Z]*[A-Z])(?=[^\s.,!@#$%^+=_]*[.,!@#$%^+=_])(?!\S*(\S)\1)(?!\S*(\S{2})\S*\2)\S{8,20}$
The pattern matches:
^
Start of string(?=[^\d\s]*\d)
Assert a digit to the right using a negated character class to match optional non whitespace charaters excluding a digit first(?=[^\sa-z]*[a-z])
Same mechanism for a char a-z(?=[^\sA-Z]*[A-Z])
Same for a char A-Z(?=[^\s.,!@#$%^+=_]*[.,!@#$%^+=_])
As well as for a character that you consider special(?!\S*(\S)\1)
Assert that there is no occurrence of 2 consecutive same non whitespace characters to the right(?!\S*(\S{2})\S*\2)
Assert that there is no sequence of 2 of the same non whitespace characters (separated by optional non whitespace characters in between) to the right\S{8,20}
$
End of stringSee a regex demo