[英]Java Regex expressions for Passwords
我剛剛創建了此類用於密碼驗證
/*
* ^ Start anchor
* (?=.*[A-Z].*[A-Z]) Ensure string has two uppercase letters.
* (?=.*[!@#$&*]) 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])(?=.*[!@#$&*])(?=.*[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));
}
嘗試這個:
(?=(?:.*[AZ]){2})(?=(?:.*[az]){2})(?=(?:.*[0-9]){2})(?=(?:.*[!@#$&*]){1}).{8}
基本思想是:
(?=(?:.*[GROUP]){NUMBER})
其中GROUP
是您要匹配的分組(即AZ
), NUMBER
是多少。
另請注意,如果要匹配每個組中NUMBER
或更多匹配項{NUMBER,}
則可以使用{NUMBER,}
。
嘗試這個希望對您有幫助。
正則表達式: ^(?=.*?[AZ].*?[AZ])(?=.*?[az].*?[az].*?[az])(?=.*?[\\d].*?[\\d])(?=.*?[^\\w]).{8}$
1.
^
字符串的開頭。2.
(?=.*?[AZ].*?[AZ])
正面看兩個大寫字符。3.
(?=.*?[az].*?[az].*?[az])
正向看三個小寫字符。4.
(?=.*?[\\d].*?[\\d])
積極向前看兩位數。5.
(?=.*?[^\\w])
積極展望非單詞字符6
.{8}
將精確匹配8個字符。7.
$
字符串結尾。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.