RegEx to match characters from list except if characters another list are present

Viewed 61

I'm trying to build a regex to validate passwords in my application.

This is PCRE (php).

My regex so far looks like this

/^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#\$%\^&\*\(\)_\+\-\=\[\]\{\}\|'])(?=.*[^\\\/])(?=.{8,20})/

And my criteria are the following

minimum length:            8
maximum length:            20
require special character: true
require uppercase:         true
require lowercase:         true
require number:            true
special character set:     !@#$%^&*()_+-=[]{}|'
excluded character set:    \/

Everything works but the excluded character set. The idea is the password must not contain any of the characters specified in that list.

Thanks for the help

1 Answers

Use

/^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&*()_\-+=[\]{}|'])[^\/]{8,20}$/

See proof

Alternative:

/^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&*()_\-+=[\]{}|'])[A-Za-z0-9!@#$%^&*()_+\-=[\]{}|']{8,20}$/

See another proof

minimum length:            8                    <= {8,20}
maximum length:            20                   <= {8,20}
require special character: true                 <= (?=.*[!@#$%^&*()_\-+=[\]{}|'])
require uppercase:         true                 <= (?=.*[A-Z])
require lowercase:         true                 <= (?=.*[a-z])
require number:            true                 <= (?=.*[0-9])
special character set:     !@#$%^&*()_+-=[]{}|' <= [^\/]
excluded character set:    \/                   <= [^\/]
Related