I’m a newbie to regex and trying to create a regex to validate pass phrases. I would like to validate that the pass phrase has:
- n number of words or more
- words should be separated by spaces
- words, each with n characters or more
- a number in at least one of the words
- at least one special character in one of the words
This is what what I have so far
^(?=.*?((?:.*?\b[a-zA-Z0-9]{2,40}\b)\s*){3,})(?=.*?[@#$%^&+=])(?=.*?[0-9]).*$
It matches this Pe2sI#sHy?ThYulU# phrase that does not have at least 3 words (no spaces). What am I doing wrong?
You should use
\s+instead of\s*. The latter allows zero spaces, the former requires at least one. But your regex is overly complicated. Try this: