正则表达式--密码复杂度验证--必须包含大写、小写、数字、特殊字符中的至少三项
【摘要】
密码复杂度要求:
大写字母、小写字母、数字、特殊字符,四项中至少包含三项。
import org.junit.Test;import org.springframework.util.StringUtils; import java.util.ArrayList;import java.util.Arrays;import java...
密码复杂度要求:
大写字母、小写字母、数字、特殊字符,四项中至少包含三项。
-
import org.junit.Test;
-
import org.springframework.util.StringUtils;
-
-
import java.util.ArrayList;
-
import java.util.Arrays;
-
import java.util.List;
-
-
/**
-
* @Author TeacherFu
-
* @Version 1.0
-
*/
-
public class PasswordTest {
-
/**
-
* 1.全部包含:大写、小写、数字、特殊字符;
-
* 2.无大写:小写、数字、特殊字符;
-
* 3.无小写:大写、数字、特殊字符;
-
* 4.无数字:大写、小写、特殊字符;
-
* 5.无特殊字符:大写、小写、数字;
-
*/
-
@Test
-
public void complexTest(){
-
List<String> list = Arrays.asList(new String[]{
-
//全包含
-
"aBcd12@",
-
"aB2@dfgh",
-
"aB2_dfgh",
-
"123Abc_",
-
"_123Abc",
-
"~123Abc",
-
-
//无大写
-
"abcd12@",
-
"!abcd12",
-
"(abcd12",
-
")abcd12",
-
"{abcd12",
-
"}abcd12",
-
"[abcd12",
-
"]abcd12",
-
"|abcd12",
-
"\\abcd12",
-
"\"abcd12",
-
"'abcd12",
-
":abcd12",
-
"?abcd12",
-
"<abcd12",
-
">abcd12",
-
"~abcd12",
-
"3~bcd12",
-
"F~bcd12",
-
-
//无小写
-
"ABCD12@",
-
"!ABCD12",
-
"!AB12333",
-
-
//无数字
-
"aBcd_@",
-
"!Abcd",
-
-
//无特殊字符
-
"abCd12",
-
"abcD12",
-
-
"12345678",
-
"abcdefgh",
-
"ABCDEFGH",
-
"abcd1234",
-
"ABCD1234",
-
-
//无特殊字符
-
"aBcd1234",
-
-
//无数字
-
"abcdEfgh!",
-
-
"~!@#$%^&*()_+{}:'?<>",
-
"abcd!@#$",
-
"1234!@#$",
-
"",
-
null
-
-
});
-
-
List<String> matches = new ArrayList<String>();
-
-
for(String word : list){
-
//
-
if(isComplexityMatches(word)) {
-
matches.add(word);
-
}
-
}
-
-
System.out.println(Arrays.toString(matches.toArray()));
-
-
List<String> matches2 = new ArrayList<String>();
-
for(String word : list){
-
//
-
if(isComplexityMatches2(word)) {
-
matches2.add(word);
-
}
-
}
-
System.out.println(Arrays.toString(matches2.toArray()));
-
-
List<String> matches3 = new ArrayList<String>();
-
for(String word : list){
-
//
-
if(isComplexityMatches3(word,5,6)) {
-
matches3.add(word);
-
}
-
}
-
System.out.println(Arrays.toString(matches3.toArray()));
-
}
-
-
/**
-
* 复杂度要求:
-
* 大写、小写、数字、特殊字符,需要包含其中至少三项
-
*
-
* @param content
-
* @return
-
*/
-
private boolean isComplexityMatches(String content){
-
-
if(!StringUtils.hasLength(content)){
-
return false;
-
}
-
-
//1.全部包含:大写、小写、数字、特殊字符;
-
String regex1 = "(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[\\W_])^.*$";
-
-
//2.无大写:小写、数字、特殊字符;
-
String regex2 = "(?=.*[a-z])(?=.*[0-9])(?=.*[\\W_])^.*$";
-
-
//3.无小写:大写、数字、特殊字符;
-
String regex3 = "(?=.*[A-Z])(?=.*[0-9])(?=.*[\\W_])^.*$";
-
-
//4.无数字:大写、小写、特殊字符;
-
String regex4 = "(?=.*[A-Z])(?=.*[a-z])(?=.*[\\W_])^.*$";
-
-
//5.无特殊字符:大写、小写、数字;
-
String regex5 = "(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])^.*$";
-
-
String regex = "(" + regex1 + ")|(" + regex2 + ")|(" + regex3 + ")|(" + regex4 + ")|(" + regex5 + ")";
-
-
return content.matches(regex);
-
}
-
-
private boolean isComplexityMatches2(String content){
-
if(!StringUtils.hasLength(content)){
-
return false;
-
}
-
//String regex = "^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\\W_!@#$%^&*`~()-+=]+$)(?![a-z0-9]+$)(?![a-z\\W_!@#$%^&*`~()-+=]+$)(?![0-9\\W_!@#$%^&*`~()-+=]+$)[a-zA-Z0-9\\W_!@#$%^&*`~()-+=]{8,30}$";
-
//String regex = "^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\\W_]+$)(?![a-z0-9]+$)(?![a-z\\W_]+$)(?![0-9\\W_]+$)[a-zA-Z0-9\\W_]{8,30}$";
-
//String regex = "^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\\W_]+$)(?![a-z0-9]+$)(?![a-z\\W_]+$)(?![0-9\\W_]+$)[a-zA-Z0-9\\W_]{5,30}$";
-
//String regex = "^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\\W_]+$)(?![a-z0-9]+$)(?![a-z\\W_]+$)(?![0-9\\W_]+$)[a-zA-Z0-9\\W_]{5,}$";
-
String regex = "^(?![A-Za-z]+$)(?![A-Z0-9]+$)(?![A-Z\\W_]+$)(?![a-z0-9]+$)(?![a-z\\W_]+$)(?![0-9\\W_]+$)[A-Za-z0-9\\W_]{5,}$";//ok
-
//String regex = "(?![A-Za-z]+$)(?![A-Z0-9]+$)(?![A-Z\\W_]+$)(?![a-z0-9]+$)(?![a-z\\W_]+$)(?![0-9\\W_]+$)^[A-Za-z0-9\\W_]{5,}$";//ok
-
//String regex = "^[A-Za-z0-9\\W_]{5,}$(?![A-Za-z]+$)(?![A-Z0-9]+$)(?![A-Z\\W_]+$)(?![a-z0-9]+$)(?![a-z\\W_]+$)";
-
-
//错误的模式,测试结果不正确(此模式匹配的是:大写、小写、数字、特殊字符等四项必须全部包含)
-
String regex2 = "^(?![A-Za-z0-9]+$)(?![a-z0-9\\W]+$)(?![A-Za-z\\W]+$)(?![A-Z0-9\\W]+$)[a-zA-Z0-9\\W]{5,30}$";
-
-
return content.matches(regex);
-
//return content.matches(regex2);
-
}
-
-
private boolean isComplexityMatches3(String content,Integer minLength,Integer maxLength){
-
if(!StringUtils.hasLength(content)){
-
return false;
-
}
-
-
if(minLength != null && maxLength != null && minLength > maxLength){
-
System.out.println("参数非法:最小长度不能大于最大长度。");
-
return false;
-
}
-
-
if(minLength == null && maxLength != null && maxLength < 0){
-
System.out.println("参数非法:最大长度不能小于0。");
-
return false;
-
}
-
-
String length = "";
-
if(minLength == null || minLength < 1){
-
length = "{0,";
-
}else{
-
length = "{" + minLength + ",";
-
}
-
if(maxLength == null){
-
length = length + "}";
-
}else {
-
length = length + maxLength + "}";
-
}
-
-
//String regex = "^(?![A-Za-z]+$)(?![A-Z0-9]+$)(?![A-Z\\W_]+$)(?![a-z0-9]+$)(?![a-z\\W_]+$)(?![0-9\\W_]+$)[A-Za-z0-9\\W_]{5,}$";//ok
-
String regex = "^(?![A-Za-z]+$)(?![A-Z0-9]+$)(?![A-Z\\W_]+$)(?![a-z0-9]+$)(?![a-z\\W_]+$)(?![0-9\\W_]+$)[A-Za-z0-9\\W_]" + length + "$";
-
-
return content.matches(regex);
-
}
-
}
文章来源: laoshifu.blog.csdn.net,作者:红目香薰,版权归原作者所有,如需转载,请联系作者。
原文链接:laoshifu.blog.csdn.net/article/details/121064278
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)