因为是要都包含所以没能一次性判断,就分开挨个判断,以下是代码,内含注释,应该很好理解。

  /**
     * 判断是否符合规则
     * @param msg
     * @return
     */
    public static boolean isMatchPwd(String msg) {

        if (TextUtils.isEmpty(msg))
            return false;

        boolean b1 = hasNum(msg);//是否包含数字
        boolean b2 = hasUpLetter(msg);//是否包含大写字母
        boolean b3 = hasLowLetter(msg);//是否包含小写字母
        boolean b4 = hasSpecialChar(msg);//是否包含特殊字符
        boolean b = b1 & b2 & b3 & b4;
        System.out.println("b1:" + b1 + ",b2:" + b2 + ",b3:" + b3 + ",b4:" + b4);
        System.out.println("是否符合:" + b);
        return b;
    }


    //是否包含数字,
    public static boolean hasNum(String content) {
        String pattern = ".*[0-9]+.*$";
        return Pattern.matches(pattern, content);
    }

    //是否包含大写字母
    public static boolean hasUpLetter(String content) {
        String pattern = ".*[A-Z]+.*";
        return Pattern.matches(pattern, content);
    }

    //是否包含小写字母
    public static boolean hasLowLetter(String content) {
        String pattern = ".*[a-z]+.*";
        return Pattern.matches(pattern, content);
    }

    //是否包含特殊字符  没有需要的可以增加,多余了可以删除掉
    public static boolean hasSpecialChar(String content) {
        String pattern = ".*[`_~!@#$%^&*()+=|{}':;',\\\\[\\\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]+.*";
        return Pattern.matches(pattern, content);
    }

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐