正则表达式验证由数字,大写字母,小写字母,特殊符,至少其中三种组成密码

        公司项目突然要求密码强度,设置密码必须符合由数字,大写字母,小写字母,特殊符,至少其中三种组成密码,子移动端需要验证,之前也没深入研究过正则表达式,在网上找了好多也没有相符合的,就花了点时间自己总结了一套,我在这里分享出来,希望对大家有帮助

  1. <span style=“font-size:18px;”>^(?![A-Za-z]+ ) ( ? ! [ A Z d ] + )(?![A-Z\\W]+ ) ( ? ! [ a z d ] + )(?![a-z\\W]+ ) ( ? ! [ d W ] + )\\S{8,20} &lt;/span&gt;&nbsp;&nbsp;</span></span></li></ol></div><pre class="java" name="code" style="display: none;"><span style="font-size:18px;">^(?![A-Za-z]+ )(?![A-Z\\d]+ ) ( ? ! [ A Z W ] + )(?![a-z\\d]+ ) ( ? ! [ a z W ] + )(?![\\d\\W]+ ) S 8 , 20

    以上代码已转译,可直接用

     解释:

           [A-Za-z]+$ 表示字符串是由大写字母和小写字母组成

        ![A-Za-z]+$ 表示字符串不全是大写字母和小写字母组成

        (?![A-Za-z]+$) 表示如果从当前匹配位置开始到结尾是一个不全是大写字母和小写字母组成的字符串,就匹配,否则匹配位置保持不变,执行接下来的表达式


    之后其他表达式都相同

    1. <span style=“font-size:18px;”>\\S{8,20}表示字符串是8-20位</span>  
    \\S{8,20}表示字符串是8-20位


    1. public static boolean PwdFormat(String pwd) {  
    2.     Pattern p = Pattern.compile(  
    3.             ”^(?![A-Za-z]+ ) ( ? ! [ A Z d ] + )(?![A-Z\\W]+ ) ( ? ! [ a z d ] + )(?![a-z\\W]+ ) ( ? ! [ d W ] + )\\S{8,20}$”);  
    4.     Matcher m = p.matcher(pwd);  
    5.     if (m.find()) {  
    6.         return true;  
    7.     }  
    8.     return false;  
    9. }  
       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;
        }


        公司项目突然要求密码强度,设置密码必须符合由数字,大写字母,小写字母,特殊符,至少其中三种组成密码,子移动端需要验证,之前也没深入研究过正则表达式,在网上找了好多也没有相符合的,就花了点时间自己总结了一套,我在这里分享出来,希望对大家有帮助

  1. <span style=“font-size:18px;”>^(?![A-Za-z]+ ) ( ? ! [ A Z d ] + )(?![A-Z\\W]+ ) ( ? ! [ a z d ] + )(?![a-z\\W]+ ) ( ? ! [ d W ] + )\\S{8,20} &lt;/span&gt;&nbsp;&nbsp;</span></span></li></ol></div><pre class="java" name="code" style="display: none;"><span style="font-size:18px;">^(?![A-Za-z]+ )(?![A-Z\\d]+ ) ( ? ! [ A Z W ] + )(?![a-z\\d]+ ) ( ? ! [ a z W ] + )(?![\\d\\W]+ ) S 8 , 20

    以上代码已转译,可直接用

     解释:

           [A-Za-z]+$ 表示字符串是由大写字母和小写字母组成

        ![A-Za-z]+$ 表示字符串不全是大写字母和小写字母组成

        (?![A-Za-z]+$) 表示如果从当前匹配位置开始到结尾是一个不全是大写字母和小写字母组成的字符串,就匹配,否则匹配位置保持不变,执行接下来的表达式


    之后其他表达式都相同

    1. <span style=“font-size:18px;”>\\S{8,20}表示字符串是8-20位</span>  
    \\S{8,20}表示字符串是8-20位


    1. public static boolean PwdFormat(String pwd) {  
    2.     Pattern p = Pattern.compile(  
    3.             ”^(?![A-Za-z]+ ) ( ? ! [ A Z d ] + )(?![A-Z\\W]+ ) ( ? ! [ a z d ] + )(?![a-z\\W]+ ) ( ? ! [ d W ] + )\\S{8,20}$”);  
    4.     Matcher m = p.matcher(pwd);  
    5.     if (m.find()) {  
    6.         return true;  
    7.     }  
    8.     return false;  
    9. }  
       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;
        }


猜你喜欢

转载自blog.csdn.net/qq_24143469/article/details/79713894
今日推荐