我刚刚创建此类密码验证Java的正则表达式表达式密码
/*
*^ Start anchor
* (?=.*[A-Z].*[A-Z]) Ensure string has two uppercase letters.
* (?=.*[[email protected]#$&*]) Ensure string has one special case letter.
* (?=.*[0-9].*[0-9]) Ensure string has two digits.
* (?=.*[a-z].*[a-z].*[a-z]) Ensure string has three lowercase letters.
* .{8} Ensure string is of length 8.
* $ End anchor.
*/
public class PasswordUtils {
private static final String PWD_REGEX = "^(?=.*[A-Z].*[A-Z])(?=.*[[email protected]#$&*])(?=.*[0-9].*[0-9])(?=.*[a-z].*[a-z].*[a-z])";
/**
* Non instantiable.
*/
private PasswordUtils() {
throw new AssertionError("Non instantiable");
}
public static boolean match (String pwd1, String pwd2) {
return StringUtils.equals(pwd1, pwd2);
}
public static boolean isStrong(String password){
return password.matches(PWD_REGEX);
}
}
那么这Junit的,但它似乎PWD不符合要求的正则表达式
private final String PWD6 = "Pi89pR&s";
@Test
public void testStrong() {
assertTrue(PasswordUtils.isStrong(PWD6));
}
它更容易不使用正则表达式。只需一次迭代字符串中的一个字符,计算大写字母,小写字母的数量等,然后确保计数符合您的标准。 –