作者:whisper
链接:https://www.proprogrammar.com/article/984
声明:请尊重原作者的劳动,如需转载请注明出处
作者:YaoHHH
链接:https://blog.csdn.net/yh18668197127/article/details/85956811
来源:CSDN
String password="Windows";
String regex="^(?=W)\\w+(?<=s)$";
Pattern p=Pattern.compile(regex);
Matcher m=p.matcher(password);
boolean isMatch=m.matches();
System.out.println(isMatch);
String password2="Windows";
String regex2="^\\w{1}(?=i)\\w+$";
Pattern p2=Pattern.compile(regex2);
Matcher m2=p2.matcher(password2);
boolean isMatch2=m2.matches();
System.out.println(isMatch2);
String password3="Windows";
String regex3="^(?!a)\\w+(?<!a)$";
Pattern p3=Pattern.compile(regex3);
Matcher m3=p.matcher(password3);
boolean isMatch3=m.matches();
System.out.println(isMatch3);
String password4="Windows";
String regex4="^\\w{1}(?!a)\\w+$";
Pattern p4=Pattern.compile(regex4);
Matcher m4=p4.matcher(password4);
boolean isMatch4=m4.matches();
System.out.println(isMatch4);
打印结果:
(?=params)表示正序的该位必须匹配params
(?!params)表示正序的该位必须不匹配params
(?<=params)表示用在末尾必须匹配params
(?<!params)表示用在末尾必须不匹配params
来看一个复杂的密码匹配:
6-16位数字字母混合,不能全为数字,不能全为字母,首位不能为数字
public boolean isPassword(String password){
String regex="^(?![0-9])(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$";
Pattern p=Pattern.compile(regex);
Matcher m=p.matcher(password);
boolean isMatch=m.matches();
Log.i(TAG, "isPassword: 是否密码正则匹配"+isMatch);
return isMatch;
}
RuntimeTest runtimeTest=new RuntimeTest();
runtimeTest.isPassword("abcABC123");
runtimeTest.isPassword("abcdefg123456789");
runtimeTest.isPassword("abcABC");
runtimeTest.isPassword("123456");
runtimeTest.isPassword("123abcABC");
查看打印结果:
符合
亲爱的读者:有时间可以点赞评论一下
月份 | 原创文章数 |
---|---|
202206 | 4 |
202205 | 2 |
202204 | 1 |
202203 | 11 |
202201 | 2 |
202108 | 7 |
202107 | 3 |
202106 | 16 |
202105 | 10 |
202104 | 16 |
202103 | 56 |
202102 | 14 |
202010 | 3 |
202009 | 3 |
202008 | 7 |
202007 | 7 |
202006 | 10 |
202005 | 11 |
202004 | 22 |
202003 | 52 |
202002 | 44 |
202001 | 83 |
201912 | 52 |
201911 | 29 |
201910 | 41 |
201909 | 99 |
201908 | 35 |
201907 | 73 |
201906 | 121 |
201811 | 1 |
201810 | 2 |
201804 | 1 |
201803 | 1 |
201802 | 1 |
201707 | 1 |
全部评论