[英]Password validation in Java using RegEx
I need a regular expression to check that if a password contains:我需要一个正则表达式来检查密码是否包含:
I tried multiple approach, but without success.我尝试了多种方法,但没有成功。 I don't know if I can do this verify with regex.
我不知道我是否可以使用正则表达式进行验证。 The most accurate pattern is:
^(?=(.*[a-zA-Z]){1,6})(?=(.*[0-9]){1,10})(?=(.*[.@#$%^&*()\-__+,]){0,1})
最准确的模式是:
^(?=(.*[a-zA-Z]){1,6})(?=(.*[0-9]){1,10})(?=(.*[.@#$%^&*()\-__+,]){0,1})
But it don't work good when I have more than 6 char, 10 digits or 1 special character.但是当我有超过 6 个字符、10 个数字或 1 个特殊字符时,它就不能正常工作了。
Could anyone help me?谁能帮帮我?
I might not use a single regex pattern, but would instead use multiple checks.我可能不会使用单一的正则表达式模式,而是会使用多个检查。 For example:
例如:
String password = "ABC123@";
int numChars = password.replaceAll("(?i)[^A-Z]+", "").length();
int numDigits = password.replaceAll("\\D+", "").length();
int numSpecial = password.replaceAll("[^!@#$%^&*()_+.-]", "").length();
if (numChars >=1 && numChars <= 6 && numDigits >= 1 && numDigits <= 10 &&
numSpecial <= 1) {
System.out.println("password is valid");
}
else {
System.out.println("password is invalid");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.