[英]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.