[英]Java Regex expressions for Passwords
I've just created this class for a password validation 我刚刚创建了此类用于密码验证
/*
* ^ 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);
}
}
Then this Junit, but it seems that the pwd does not match the requirements of the regular expression 然后是这个Junit,但是看来pwd与正则表达式的要求不匹配
private final String PWD6 = "Pi89pR&s";
@Test
public void testStrong () {
assertTrue(PasswordUtils.isStrong(PWD6));
}
Try this: 尝试这个:
(?=(?:.*[AZ]){2})(?=(?:.*[az]){2})(?=(?:.*[0-9]){2})(?=(?:.*[!@#$&*]){1}).{8}
The basic idea is: 基本思想是:
(?=(?:.*[GROUP]){NUMBER})
Where GROUP
is the grouping you want to match (ie AZ
) and NUMBER
is how many. 其中
GROUP
是您要匹配的分组(即AZ
), NUMBER
是多少。
Also note, you can use {NUMBER,}
if you want to match NUMBER
or more occurrences of each group. 另请注意,如果要匹配每个组中
NUMBER
或更多匹配项{NUMBER,}
则可以使用{NUMBER,}
。
https://regex101.com/r/JGtIkF/1 https://regex101.com/r/JGtIkF/1
Try this hope this will help you out.. 尝试这个希望对您有帮助。
Regex: ^(?=.*?[AZ].*?[AZ])(?=.*?[az].*?[az].*?[az])(?=.*?[\\d].*?[\\d])(?=.*?[^\\w]).{8}$
正则表达式:
^(?=.*?[AZ].*?[AZ])(?=.*?[az].*?[az].*?[az])(?=.*?[\\d].*?[\\d])(?=.*?[^\\w]).{8}$
1.
^
start of string.1.
^
字符串的开头。2.
(?=.*?[AZ].*?[AZ])
positive look ahead for two uppercase characters.2.
(?=.*?[AZ].*?[AZ])
正面看两个大写字符。3.
(?=.*?[az].*?[az].*?[az])
positive look ahead for three lowercase characters.3.
(?=.*?[az].*?[az].*?[az])
正向看三个小写字符。4.
(?=.*?[\\d].*?[\\d])
positive look ahead for two digits.4.
(?=.*?[\\d].*?[\\d])
积极向前看两位数。5.
(?=.*?[^\\w])
positive look ahead for non-word character5.
(?=.*?[^\\w])
积极展望非单词字符6.
.{8}
will match exactly 8 characters.6
.{8}
将精确匹配8个字符。7.
$
end of string.7.
$
字符串结尾。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.