首页 > 编程语言 >java正则表达式校验密码必须是包含大小写字母、数字、特殊符号的8位以上组合 或其中至少二种,三种,四种

java正则表达式校验密码必须是包含大小写字母、数字、特殊符号的8位以上组合 或其中至少二种,三种,四种

时间:2023-02-20 09:34:24浏览次数:41  
标签:java String 小写字母 zA matches 匹配 password 特殊符号 数字

一、语法
字符 说明
\ 将下一字符标记为特殊字符、文本、反向引用或八进制转义符。例如, n匹配字符 n。\n 匹配换行符。序列 \\\\ 匹配 \\ ,\\( 匹配 (。
^ 匹配输入字符串开始的位置。如果设置了 RegExp 对象的 Multiline 属性,^ 还会与"\n"或"\r"之后的位置匹配。
$ 匹配输入字符串结尾的位置。如果设置了 RegExp 对象的 Multiline 属性,$ 还会与"\n"或"\r"之前的位置匹配。
* 零次或多次匹配前面的字符或子表达式。例如,zo* 匹配"z"和"zoo"。* 等效于 {0,}。
+ 一次或多次匹配前面的字符或子表达式。例如,"zo+"与"zo"和"zoo"匹配,但与"z"不匹配。+ 等效于 {1,}。
? 零次或一次匹配前面的字符或子表达式。例如,"do(es)?“匹配"do"或"does"中的"do”。? 等效于 {0,1}。
{n} _n _是非负整数。正好匹配 n 次。例如,"o{2}"与"Bob"中的"o"不匹配,但与"food"中的两个"o"匹配。
{n,} _n _是非负整数。至少匹配 _n _次。例如,"o{2,}“不匹配"Bob"中的"o”,而匹配"foooood"中的所有 o。"o{1,}“等效于"o+”。"o{0,}“等效于"o*”。
{n,m} m 和 n 是非负整数,其中 n <= m。匹配至少 n 次,至多 m 次。例如,"o{1,3}"匹配"fooooood"中的头三个 o。‘o{0,1}’ 等效于 ‘o?’。注意:您不能将空格插入逗号和数字之间。
? 当此字符紧随任何其他限定符(*、+、?、{n}、{n,}、{n,m})之后时,匹配模式是"非贪心的"。"非贪心的"模式匹配搜索到的、尽可能短的字符串,而默认的"贪心的"模式匹配搜索到的、尽可能长的字符串。例如,在字符串"oooo"中,"o+?“只匹配单个"o”,而"o+“匹配所有"o”。

二、正则实战

1、纯字母
"[a-zA-Z]{1,}$"

2、纯数字
"[0-9]{1,}$"

3、字母和数字组合
"((^[a-zA-Z]{1,}[0-9]{1,}[a-zA-Z0-9]*)+)|((^[0-9]{1,}[a-zA-Z]{1,}[a-zA-Z0-9]*)+)$"

4、字母或数字
"[a-zA-Z0-9]+$"

5、字母、数字、下划线,都可以
"\\w+$"

6、字母、数字、特殊符号,至少匹配2种
/**
* 假定设置密码时,密码规则为: 字母、数字、特殊符号,至少匹配2种
* 则密码可能出现的情况有:
* 1、数字+特殊符号
* 2、字母+特殊符号
* 3、字母+数字
* 4、字母+数字+特殊符号
* (组合与顺序无关)
* 解决思路:
* 1、遍历字符串的字符数组,查看是否包含目标特殊字符,若包含,则标记字符串
* 包含特殊字符,并替换当前特殊字符为''。
* 2、判断剩下的字符组成的字符串,是否匹配以下情况
* - 纯字母
* - 纯数字
* - 字母+数字
* 3、字符串匹配规则
* 纯字母+包含特殊字符 ---- 匹配通过
* 纯数字+包含特殊字符 ---- 匹配通过
* 字母+数字+包含个数字符 ---- 匹配通过
*/
//特殊字符
public static final String SPEC_CHARACTERS = " !\"#$%&'()*+,-./:;<=>?@\\]\\[^_`{|}~";
// 纯字母
public static final String character = "[a-zA-Z]{1,}$";
// 纯数字
public static final String numberic = "[0-9]{1,}$";
// 字母和数字
public static final String number_and_character = "((^[a-zA-Z]{1,}[0-9]{1,}[a-zA-Z0-9]*)+)" +
"|((^[0-9]{1,}[a-zA-Z]{1,}[a-zA-Z0-9]*)+)$";
// 字母或数字
public static final String number_or_character = "[a-zA-Z0-9]+$";
// 字母数字下划线
public static final String ncw = "\\w+$";

public static boolean checkPassword(String targetString) {
String opStr = targetString;
boolean isLegal = false;
boolean hasSpecChar = false;
char[] charArray = opStr.toCharArray();
for (char c : charArray) {
if (SPEC_CHARACTERS.contains(String.valueOf(c))) {
hasSpecChar = true;
// 替换此字符串
opStr = opStr.replace(c, ' ');
}
}
String excSpecCharStr = opStr.replace(" ", "");
boolean isPureNum = Pattern.compile(numberic).matcher(excSpecCharStr).matches();
boolean isPureChar = Pattern.compile(character).matcher(excSpecCharStr).matches();
boolean isNumAndChar = Pattern.compile(number_and_character).matcher(excSpecCharStr).matches();
isLegal = ((isPureNum && hasSpecChar)
|| (isPureChar && hasSpecChar) || isNumAndChar && hasSpecChar) || isNumAndChar;
System.out.println("字符串:" + targetString + ",是否符合规则:" + isLegal);
System.out.println("---------------");
return isLegal;
}

public static void main(String[] args) {
checkPassword("fasdagd");
checkPassword("41234123");
checkPassword("#$%^&&*(");
checkPassword("fasd$$");
checkPassword("41234%%%");
checkPassword("fasd41^(324");
checkPassword("fa413%^&*");
checkPassword("&%fa413%^&*");
}

附:密码中允许出现数字、大写字母、小写字母,但至少包含其中2种且长度在8-16之间(三种符号任取其二)
包含三种符号中的一种符号,无效。
包含三种符号中的二种符号,有效。
包含三种符号中的三种符号,有效。
正向否定方式:

^(?![\d]+$)(?![a-z]+$)(?![A-Z]+$)[\da-zA-z]{8,16}$
(?![\d]+$):正向否定查找。匹配向后直到结尾均为数字的字符。
^(?![\d]+$)(?![a-z]+$)(?![A-Z]+$):匹配向后直到结尾既不是纯数字,又不是纯消息字母,又不是纯大写字母的开始字符。
console> console.log(/^(?![\d]+$)(?![a-z]+$)(?![A-Z]+$)[\da-zA-z]{8,16}$/g.test("abcdefgh"));
false
console> console.log(/^(?![\d]+$)(?![a-z]+$)(?![A-Z]+$)[\da-zA-z]{8,16}$/g.test("abcdefgH"));
true
console> console.log(/^(?![\d]+$)(?![a-z]+$)(?![A-Z]+$)[\da-zA-z]{8,16}$/g.test("abcdefg8"));
true
console> console.log(/^(?![\d]+$)(?![a-z]+$)(?![A-Z]+$)[\da-zA-z]{8,16}$/g.test("abcdefG8"));
true
console> console.log(/^(?![\d]+$)(?![a-z]+$)(?![A-Z]+$)[\da-zA-z]{8,16}$/g.test("12345678"));
false
console> console.log(/^(?![\d]+$)(?![a-z]+$)(?![A-Z]+$)[\da-zA-z]{8,16}$/g.test("12345678a"));
true
这个表达式也可以实现相同的功能

^(?!^(\d+|[a-z]+|[A-Z]+)$)[\da-zA-Z]{8,16}$
^(\d+|[a-z]+|[A-Z]+)$:从头到尾要么是纯数字,要么是纯小写字母,要么是纯大写字母。
^(?!^(\d+|[a-z]+|[A-Z]+)$):匹配向后直到结尾既不是纯数字,又不是纯消息字母,又不是纯大写字母的开始字符。

总结
到此这篇关于Java正则校验密码至少包含字母数字特殊符号中的2种的文章就介绍到这了,更多相关Java正则校验密码内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

 

一、排除法和详细解释

  1、排除大写字母、小写字母、数字、特殊符号中1种组合、2种组合、3种组合,那么就只剩下4种都包含的组合了

  2、表达式为:^(?![A-Za-z0-9]+$)(?![a-z0-9\\W]+$)(?![A-Za-z\\W]+$)(?![A-Z0-9\\W]+$)[a-zA-Z0-9\\W]{8,}$

  3、拆分解释:其中(2)-(6)运用了零宽断言、环视等正则功能

    (1)^匹配开头

    (2)(?![A-Za-z0-9]+$)匹配后面不全是(大写字母或小写字母或数字)的位置,排除了(大写字母、小写字母、数字)的1种2种3种组合

    (3)(?![a-z0-9\\W]+$)同理,排除了(小写字母、数字、特殊符号)的1种2种3种组合

    (4)(?![A-Za-z\\W]+$)同理,排除了(大写字母、小写字母、特殊符号)的1种2种3种组合

    (5)(?![A-Z0-9\\W]+$)同理,排除了(大写字母、数组、特殊符号)的1种2种3种组合

    (6)[a-zA-Z0-9\\W]匹配(小写字母或大写字母或数字或特殊符号)因为排除了上面的组合,所以就只剩下了4种都包含的组合了

    (7){8,}8位以上

    (8)$匹配字符串结尾

二、测试代码

public class PasswordTest {
// 密码长度不少于8位且至少包含大写字母、小写字母、数字和特殊符号中的四种
public static final String password1 = "^(?![A-Za-z0-9]+$)(?![a-z0-9\\W]+$)(?![A-Za-z\\W]+$)(?![A-Z0-9\\W]+$)[a-zA-Z0-9\\W]{8,}$";
// 密码长度8-20位且至少包含大写字母、小写字母、数字或特殊符号中的任意三种
public static final String password = "^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\\W_]+$)(?![a-z0-9]+$)(?![a-z\\W_]+$)(?![0-9\\W_]+$)[a-zA-Z0-9\\W_]{8,20}$";
//密码应包括数字、小写字母、大写字母、特殊符号4类中至少3类;
String PW_PATTERN = "^(?![a-zA-Z]+$)(?![a-z\\d]+$)(?![a-z!@#\\$%]+$)(?![A-Z\\d]+$)(?![A-Z!@#\\$%]+$)(?![\\d!@#\\$%]+$)[a-zA-Z\\d!@#\\$%]+$";

//大小写字母数字下划线4选4,且至少8位
private final String regex4 = "^(?![A-Z0-9\\W_]+$)(?![a-z0-9\\W_]+$)(?![a-zA-Z\\W_]+$)(?![a-zA-Z0-9]+$)[a-zA-Z0-9\\W_]{8,}$";
//大小写字母数字下划线4选3,且至少8位
private final String regex4To3 = "^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\\W_]+$)(?![a-z0-9]+$)(?![a-z\\W_]+$)(?![0-9\\W_]+$)[a-zA-Z0-9\\W_]{8,}$";
//大小写字母数字下划线,且至少8位
private final String regexAll = "[a-zA-Z0-9\\W_]{8,}";
/**
* 正则必须包含字母数字下划线,长度至少8位
* @param str1
* @return
*/
public boolean regex5(String str1){
boolean flag = false;
flag = Pattern.matches(regex4,str1);
return flag;
}

public static void main(String[] args) {
String password1 = "ABCDEFGHIG"; //全部大写
String password2 = "abcdefghig"; //全部小写
String password3 = "0123456789"; //全部数字
String password4 = "!@#$%^&*()"; //全部特殊字符
String password5 = "ABCDEabcde"; //大写和小写
String password6 = "ABCDE01234"; //大写和数字
String password7 = "ABCDE!@#$%"; //大写和特殊字符
String password8 = "abcde01234"; //小写和数字
String password9 = "abcde!@#$%"; //小写字母和特殊字符
String password10 = "01234!@#$%"; //数字和特殊字符
String password11 = "Aa4!"; //长度不够8位数
String password12 = "ABCDE01234!@#$%"; //符合要求密码任意三种
String password13 = "ABCDEabcde!@#$%"; //符合要求密码任意三种
String password14 = "ABCDEabcde01234"; //符合要求密码任意三种
String password15 = "abcde01234!@#$%"; //符合要求密码任意三种
String password16= "ABCabc012@#"; //符合要求密码任意三种 和 符合全部的四种

System.out.println(password1.matches(password) + " 1");
System.out.println(password2.matches(password)+ " 2");
System.out.println(password3.matches(password)+ " 3");
System.out.println(password4.matches(password)+ " 4");
System.out.println(password5.matches(password)+ " 5");
System.out.println(password6.matches(password)+ " 6");
System.out.println(password7.matches(password)+ " 7");
System.out.println(password8.matches(password)+ " 8");
System.out.println(password9.matches(password)+ " 9");
System.out.println(password10.matches(password)+ " 10");
System.out.println(password11.matches(password)+ " 11");
System.out.println(password12.matches(password)+ " 12");
System.out.println(password13.matches(password)+ " 13");
System.out.println(password14.matches(password)+ " 14");
System.out.println(password15.matches(password)+ " 15");
System.out.println(password16.matches(password)+ " 16");
}
}

Java之判断密码是否是大小写字母、数字、特殊字符中的至少两种组合
public class CheckPassword {
//数字
public static final String REG_NUMBER = ".*\\d+.*";
//小写字母
public static final String REG_UPPERCASE = ".*[A-Z]+.*";
//大写字母
public static final String REG_LOWERCASE = ".*[a-z]+.*";
//特殊符号
public static final String REG_SYMBOL = ".*[~!@#$%^&*()_+|<>,.?/:;'\\[\\]{}\"]+.*";

public static boolean isPswComplex(String password){
//密码为空或者长度小于8位则返回false
if (password == null || password.length() <8 ) return false;
int i = 0;
if (password.matches(REG_NUMBER)) i++;
if (password.matches(REG_LOWERCASE))i++;
if (password.matches(REG_UPPERCASE)) i++;
if (password.matches(REG_SYMBOL)) i++;

if (i < 2 ) return false;

return true;
}

 

 

Java随机获取32位密码且必须包含大小写字母、数字和特殊字符,四种的任意三种
import java.util.Random;

public class GetRandomPwd{

/**
* @Title: getRandomPwd
* @Description:获取制定长度的密码,包含大小写字母、数字和特殊字符,四种的任意三种
* @param len
* @return String
* @throws
*/
public static String getRandomPwd(int len) {
String result = null;
while(len==32){
result = makeRandomPwd(len);
if (result.matches(".*[a-z]{1,}.*") && result.matches(".*[A-Z]{1,}.*") && result.matches(".*\\d{1,}.*") && result.matches(".*[~;<@#:>%^]{1,}.*")) {
return result;
}
result = makeRandomPwd(len);
}
return "长度不得少于32位!";
}

/**
* @Title: makeRandomPwd
* @Description:随机密码生成
* @param len
* @return String
* @throws
*/
public static String makeRandomPwd(int len) {
char charr[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890~;<@#:>%^".toCharArray();
StringBuilder sb = new StringBuilder();
Random r = new Random();
for (int x = 0; x < len; ++x) {
sb.append(charr[r.nextInt(charr.length)]);
}
return sb.toString();
}

public static void main(String[] args) {
String password = getRandomPwd(32);
System.out.println(">>>:"+password);
}
}

 

标签:java,String,小写字母,zA,matches,匹配,password,特殊符号,数字
From: https://www.cnblogs.com/wjsqqj/p/17136251.html

相关文章

  • 责任链和策略设计模式-基于Java编程语言
    作者:京东物流钟磊1前言最近在梳理接口逻辑的时候发现,代码中使用的策略和责任链设计模式给我留下了非常深刻的印象。一个业务逻辑流程通常非常适合使用责任链和策略设计......
  • java的Stream
    代码List<Student>all=Student.getAll();//转换成数组过滤所有的男性Student[]students=all.stream().filter(s->"男".equalsIgnoreCase(s.getSex()))......
  • 读Java实战(第二版)笔记15_并行数据处理与性能
    1. Stream1.1. 允许你声明性地将顺序流转变成并行流1.2. 能对这些集合执行操作流水线,可以充分利用计算机的多个核2. 并行流2.1. 把内容拆分成多个数据块,用不同线......
  • Odoo 通过Javascript调用模型中自定义方法
    实践环境Odoo14.0-20221212(CommunityEdition)代码实现在js脚本函数中调用模型中自定义方法:this._rpc({model:'demo.wizard',//模型名称,即模型类定义中_na......
  • 《深入理解java虚拟机》第七章读书笔记——虚拟机类加载机制
    系列文章目录和关于我一丶虚拟机类加载机制是什么java虚拟机将描述类的数据从class文件加载到内存,并对数据进行校验,转换解析和初始化,最终形成可用被虚拟机直接使用的jav......
  • JavaScript回调函数
    回调函数是一段可执行的代码段,它作为一个参数传递给其他的代码,其作用是在需要的时候方便调用这段(回调函数)代码。在JavaScript中函数也是对象的一种,同样对象可以作为参数传......
  • Java String为什么不可变?
    publicfinalclassStringimplementsjava.io.Serializable,Comparable<String>,CharSequence{privatefinalcharvalue[]; //...}String类中使用final......
  • Java 如何通过JDBC 操作数据库
    JDBC是Java数据库连接,即JavaDataBaseConnectivity。JDBC可让Java通过程序操作关系型数据库,可基于驱动程序实现与数据库的连接与操作。JDBC有统一的API,提供一致的......
  • Java实现对MongoDB的AND、OR和IN操作
    很全的JAVA操作mongodb:​​http://www.blogjava.net/xiaomage234/archive/2012/08/06/384904.html​​ 转的: AND:publicvoidtestAnd(){//agender='female'ANDag......
  • 常用java路径
    System.out.println(LmsFEMain.class.getClassLoader().getResource(""));//file:/E:/workspace/FEServer/bin/System.out.println(ClassLoader.getSystemResource("")......