This works. It requires at least one uppercase letter, one lowercase letter, one number, and one special character such as @
or #
or $
, with a length of at least eight characters.
(?m)^((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[\\W]).{8,})$
The (?m)
at the beginning makes sure that the .
in the regex does not match a newline.
From RegexBuddy:
^((?=.\d)(?=.[a-z])(?=.[A-Z])(?=.[\W]).{8,})$
Options: ^ and $ match at line breaks
Assert position at the beginning of a line (at beginning of the string or after a line break character) «^»
Match the regular expression below and capture its match into backreference number 1 «((?=.\d)(?=.[a-z])(?=.[A-Z])(?=.[\W]).{8,})»
- Assert that the regex below can be matched, starting at this position (positive lookahead) «(?=.*\d)»
- Match any single character that is not a line break character «.*»
- Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
- Match a single digit 0..9 «\d»
Assert that the regex below can be matched, starting at this position (positive lookahead) «(?=.*[a-z])»
- Match any single character that is not a line break character «.*»
- Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
- Match a single character in the range between “a” and “z” «[a-z]»
Assert that the regex below can be matched, starting at this position (positive lookahead) «(?=.*[A-Z])»
- Match any single character that is not a line break character «.*»
- Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
- Match a single character in the range between “A” and “Z” «[A-Z]»
Assert that the regex below can be matched, starting at this position (positive lookahead) «(?=.*[\W])»
- Match any single character that is not a line break character «.*»
- Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
- Match a single character that is a “non-word character” «[\W]»
- Match any single character that is not a line break character «.{8,}»
- Between eight and unlimited times, as many times as possible, giving back as needed (greedy) «{8,}»
Assert position at the end of a line (at the end of the string or before a line break character) «$»
It matches
abcDefg1$
1zBA^frmb
1#Basdfadsfadsf
It does not match
abcd123
123abc
abcdEFGH
abcdEFG2
abCDeF1E
1a2bc