I need to verify a password string by using Java. This is the requirement of validation:
- at least 1 number
- at least 1 alphabet character
- at least 1 character from set !@#$%^&*()_+=-~`][{};':"/.>?,<
- 8 to 20 characters
After screwing around and banging my head to the wall several times, I came up with this regular expression
if (!password.matches("^(?=.+[0-9])(?=.+[a-zA-Z])(?=.+[\\x21-\\x2F\\x3A-\\x40\\x5B-\\x60\\x7B-\\x7E])[0-9a-zA-Z\\x21-\\x2F\\x3A-\\x40\\x5B-\\x60\\x7B-\\x7E]{8,20}$")) {
}
which looks too awful and insane. Is there any better way to achieve this mission ?