我的密码条件是,最少8个字符,最少1个特殊字符,最少1个数字
为此我编写了一个简单的类来验证,但最终失败了。
非常感谢任何帮助。
public class PasswordVerifier {
private static final String SPECIAL_CHARACTERS = "(`~!@#$%^&*()_+=-][;'/.,\\<>?|:\"}{)";
public static void main(String... args) {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
try {
String password = in.readLine();
if(!password.matches("^.*(?=.{8,})(?=.*[0-9])(?=.*[SPECIAL_CHARACTERS]).*$")){
System.out.println("Password does not satisfy compliant");
} else {
System.out.println("Yes.. gets through");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
最佳答案
这可能适合您的要求:
private static final String SPECIAL_CHARACTERS = "(`~!@#$%^&*()_+=-\\]\\[;'/.,\\<>?|:\"}{)";
public static void main(String[] args) {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
try {
String password = in.readLine();
if(!password.matches("((?=.*\\d)(?=.*["+SPECIAL_CHARACTERS+"]).{8,})")){
System.out.println("Password does not satisfy compliant");
} else {
System.out.println("Yes.. gets through");
}
} catch (IOException e) {
e.printStackTrace();
}
}
正则表达式指定:
- 输入内容必须包含 0-9 之间的一位数字
- 必须包含您定义的 SPECIAL_CHARACTERS 列表中的一个特殊符号
- 长度应至少为 8 个字符
关于java - 密码模式匹配失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12337013/