Well I see a non-regex way to go about it:
public boolean isValidPassword(String s) {
int lowerCase = 0;
int upperCase = 0;
int numeric = 0;
int special = 0;
for (int c = 0; i < s.length(); i++) {
char i = s.charAt(c);
if (i >= 'A' && i <= 'Z') {
upperCase++;
} else if (i >= 'a' && i <= 'z') {
lowerCase++;
} else if (i >= '0' && i <= '9') {
numeric++;
} else {
special++;
}
//early ending case
return lowerCase > 1 && upperCase > 1 && numeric > 0;
}
return false;
}
If you wanted to you could abstract the early ending predicate using a functional interface:
@FunctionalInterface
private static interface PasswordMatch {
public boolean match(int lowerCase, int upperCase, int numbers, int special);
}
public boolean isValidPassword(String s, PasswordMatch matcher) {
//...
//in loop
if (matcher.match(lowerCase, upperCase, numeric, special) {
return true;
}
//...
}
Thus making the call mutable to the situation:
if (isValidPassword(/* some pass */, (lower, upper, number, spec)
-> lowerCase > 1 && upperCase > 1 && numeric > 0)) {
//etc...
}