Java unlike C++ has regular expression support, though c++ 11 and boost library has it
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class PasswordValidator{
private Pattern pattern;
private Matcher matcher;
private final String PASSWORD_PATTERN =
"((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})";
public PasswordValidator(){
pattern = Pattern.compile(PASSWORD_PATTERN);
}
public boolean validate(String password){
matcher = pattern.matcher(password);
return matcher.matches();
}
public static void main(String arg[]) {
PasswordValidator pv = new PasswordValidator();
String pw = "dharmac1A@";
if(pv.validate (pw)) {
System.out.println("Valid");
} else {
System.out.println("InValid");
}
pw = "iamnotvalidpassword";
if(pv.validate (pw)) {
System.out.println("Valid");
} else {
System.out.println("InValid");
}
pw = "dharmatejachalla";
if(pv.validate (pw)) {
System.out.println("Valid");
} else {
System.out.println("InValid");
}
}
}