公司项目突然要求密码强度,设置密码必须符合由数字,大写字母,小写字母,特殊符,至少其中三种组成密码,子移动端需要验证,之前也没深入研究过正则表达式,在网上找了好多也没有相符合的,就花了点时间自己总结了一套,我在这里分享出来,希望对大家有帮助
- <span style=“font-size:18px;”>^(?![A-Za-z]+
)(?![A-Z\\W]+
)(?![a-z\\W]+
)\\S{8,20}
)(?![A-Z\\d]+
)(?![a-z\\d]+
)(?![\\d\\W]+
以上代码已转译,可直接用
解释:
[A-Za-z]+$ 表示字符串是由大写字母和小写字母组成
![A-Za-z]+$ 表示字符串不全是大写字母和小写字母组成
(?![A-Za-z]+$) 表示如果从当前匹配位置开始到结尾是一个不全是大写字母和小写字母组成的字符串,就匹配,否则匹配位置保持不变,执行接下来的表达式
之后其他表达式都相同
- <span style=“font-size:18px;”>\\S{8,20}表示字符串是8-20位</span>
\\S{8,20}表示字符串是8-20位
- public static boolean PwdFormat(String pwd) {
- Pattern p = Pattern.compile(
- ”^(?![A-Za-z]+ )(?![A-Z\\W]+ )(?![a-z\\W]+ )\\S{8,20}$”);
- Matcher m = p.matcher(pwd);
- if (m.find()) {
- return true;
- }
- return false;
- }
public static boolean PwdFormat(String pwd) { Pattern p = Pattern.compile( "^(?![A-Za-z]+$)(?![A-Z\\d]+$)(?![A-Z\\W]+$)(?![a-z\\d]+$)(?![a-z\\W]+$)(?![\\d\\W]+$)\\S{8,20}$"); Matcher m = p.matcher(pwd); if (m.find()) { return true; } return false; }
公司项目突然要求密码强度,设置密码必须符合由数字,大写字母,小写字母,特殊符,至少其中三种组成密码,子移动端需要验证,之前也没深入研究过正则表达式,在网上找了好多也没有相符合的,就花了点时间自己总结了一套,我在这里分享出来,希望对大家有帮助
- <span style=“font-size:18px;”>^(?![A-Za-z]+
)(?![A-Z\\W]+
)(?![a-z\\W]+
)\\S{8,20}
)(?![A-Z\\d]+
)(?![a-z\\d]+
)(?![\\d\\W]+
以上代码已转译,可直接用
解释:
[A-Za-z]+$ 表示字符串是由大写字母和小写字母组成
![A-Za-z]+$ 表示字符串不全是大写字母和小写字母组成
(?![A-Za-z]+$) 表示如果从当前匹配位置开始到结尾是一个不全是大写字母和小写字母组成的字符串,就匹配,否则匹配位置保持不变,执行接下来的表达式
之后其他表达式都相同
- <span style=“font-size:18px;”>\\S{8,20}表示字符串是8-20位</span>
\\S{8,20}表示字符串是8-20位
- public static boolean PwdFormat(String pwd) {
- Pattern p = Pattern.compile(
- ”^(?![A-Za-z]+ )(?![A-Z\\W]+ )(?![a-z\\W]+ )\\S{8,20}$”);
- Matcher m = p.matcher(pwd);
- if (m.find()) {
- return true;
- }
- return false;
- }
public static boolean PwdFormat(String pwd) { Pattern p = Pattern.compile( "^(?![A-Za-z]+$)(?![A-Z\\d]+$)(?![A-Z\\W]+$)(?![a-z\\d]+$)(?![a-z\\W]+$)(?![\\d\\W]+$)\\S{8,20}$"); Matcher m = p.matcher(pwd); if (m.find()) { return true; } return false; }