import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class PasswordTest {
private final static Matcher passwordMatcher = Pattern.compile(
"^(?:([a-z])|([A-Z])|([0-9])|([@#$%])){8,15}$"
).matcher("");
public static void main(String[] args) {
String[] strs = {
"abcdefg12345",
"aaabbbAAA$$$",
"aaabbbAAAa@13434",
"aaAA11",
"AAAaaa113@"
};
for(int i = 0; i < strs.length; i++) {
System.out.printf("str: %-20s length: %2d result: %s%n",
strs[i],
strs[i].length(),
checkPassword(strs[i])
);
}
}
public static boolean checkPassword(String password) {
if (password == null) {
return false;
}
passwordMatcher.reset(password);
if (!passwordMatcher.matches()) {
return false;
}
int count = 0;
for (int i = passwordMatcher.groupCount(); i > 0; i--) {
if (passwordMatcher.start(i) > -1) {
count++;
}
}
return (count >= 3);
}
}