我剛剛創建此類密碼驗證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));
}
它更容易不使用正則表達式。只需一次迭代字符串中的一個字符,計算大寫字母,小寫字母的數量等,然後確保計數符合您的標準。 –