天翼云代理,天翼云代理商,北京代理商
天翼云折扣专线:400-150-1900(全国市话)

正则表达式--密码复杂度验证--必须包含大写、小写、数字、特殊字符中的至少三项

2022-01-17 12:39:02
简介: 正则表达式--密码复杂度验证--必须包含大写、小写、数字、特殊字符中的至少三项

密码复杂度要求:


大写字母、小写字母、数字、特殊字符,四项中至少包含三项。


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);  } }


12年经验 · 提供上云保障

服务热线:132-6161-6125(手机) 400-150-1900(全国市话)

站内导航: 天翼云服务器价格| 天翼云购买流程| 天翼云代理| 北京天翼云代理商| 杭州天翼云代理| 深圳天翼云代理商| 钉钉代理商| 阿里云代理| 公司官网

我公司收款账号| 天翼云备案系统

CopyRight © 2019 天翼云代理商. All Rights Reserved 京ICP备11011846号-15 管理-北京志远天辰科技有限公司