java - Regex for password matching -
i have searched site , not finding looking for. password criteria:
- must 6 characters, 50 max
- must include 1 alpha character
- must include 1 numeric or special character
here have in java:
public static pattern p = pattern.compile( "((?=.*\\d)(?=.*[a-z])(?=.*[a-z])|(?=.*[\\d~!@#$%^&*\\(\\)_+\\{\\}\\[\\]\\?<>|_]).{6,50})" );
the problem password of 1234567 matching(it valid) should not be.
any great.
make sure use matcher.matches()
method, assert whole string matches pattern.
your current regex:
"((?=.*\\d)(?=.*[a-z])(?=.*[a-z])|(?=.*[\\d~!@#$%^&*\\(\\)_+\\{\\}\\[\\]\\?<>|_]).{6,50})"
means:
- the string must contain @ least digit
(?=.*\\d)
, lower case english alphabet(?=.*[a-z])
, , upper case character(?=.*[a-z])
- or
|
string must contain @ least 1 character may digit or special character(?=.*[\\d~!@#$%^&*\\(\\)_+\\{\\}\\[\\]\\?<>|_])
- either conditions above holds true, , string must between 6 50 characters long, , not contain line separator.
the correct regex is:
"(?=.*[a-za-z])(?=.*[\\d~!@#$%^&*()_+{}\\[\\]?<>|]).{6,50}"
this check:
- the string must contain english alphabet character (either upper case or lower case)
(?=.*[a-za-z])
, , character can either digit or special character(?=.*[\\d~!@#$%^&*()_+{}\\[\\]?<>|])
- the string must between 6 , 50 characters, , not contain line separator.
note removed escaping characters, except []
, since {}?()
loses special meaning inside character class.
Comments
Post a Comment