I am new to regex.
Basically I need to validate a password in Java for the following requirement:
- The password must contain at least six characters.
- The password can contain max 20 characters
In order for the password to be valid any 3 of the 4 validation rules below should be met:
- uppercase letters of the English alphabet from A to Z;
- lowercase letters a to z;
- decimal digits (0 to 9);
- non-alphanumeric characters (such as!, $, #,%)
For example:
Q145aqa- OK since contains uppercase, lowercase and digit145AS$- OK since contains uppercase, digit and non-alphanumeric characters145234$- Not OK since contain only digit and non-alphanumeric characters and missing one other validation rule
Basically this is my regex at the moment:
^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z\d]{6,20}$
I don't how how to add the condition for non-alphanumeric characters to the expression and how I can get regex to check if any three of the above 4 validation rules are met in order for the password to be a valid one?
String#length, for the others i´d go with @WiktorStribiżew suggestion. (Wheras the single regex expression will get pretty simple by then, just check if it contains an uppercase Letter etc.) - SomeJavaGuy