regexpasswords

Regex for Password with repetitive pattern


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?


Solution

  • 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:

    See a regex demo