我有一个正则;
("(?=.*[a-z]).*")
("(?=.*[0-9]).*")
("(?=.*[A-Z]).*")
("(?=.*[!@#$%&*()_+=|<>?{}\[\]~-]).*")
检查一个有要求的密码:长度= 8,然后以下三个 - 小写,大写,数字,特殊字符。上述4 长度的3中是需要8的3个。
我已经工作的内容,直到密码中有一个空间,然后打印错误的消息。在另一词中,我如何在特殊字符列表中包含whitespace,谢谢!
您可以尝试一下:
String password = "pA55w$rd";
int counter = 0;
if(password.length() >= 8)
{
Pattern pat = Pattern.compile(".*[a-z].*"); // Lowercase
Matcher m = pat.matcher(password);
if(m.find()) counter++;
pat = Pattern.compile(".*[0-9].*"); // Digit
m = pat.matcher(password);
if(m.find()) counter++;
pat = Pattern.compile(".*[A-Z].*"); // Uppercase
m = pat.matcher(password);
if(m.find()) counter++;
pat = Pattern.compile(".*\W.*"); // Special Character
m = pat.matcher(password);
if(m.find()) counter++;
if(counter == 3 || counter == 4)
{
System.out.println("VALID PASSWORD!");
}
else
{
System.out.println("INVALID PASSWORD!");
}
}
else
{
System.out.println("INVALID PASSWORD!");
}
有两种情况:它要么与所需的长度匹配。
如果它确实与长度匹配,则一次检查4个表壳中的每一个,并每次都会增加计数器。由于您希望它与案例的3或4个匹配,因此我在那儿放了一个if-else案件。