Android 用户验证(超全正则表达)

验证中英文

public static boolean isZhongYing(String str){

      String zhongyin="^[A-Za-z\u4e00-\u9fa5]+$";//中英文

        Pattern p = Pattern.compile(zhongyin);

        return p.matcher(str).matches();

    }

验证字母数字

public static boolean isShuZiYing(String str){

        String yinshuzi="^[A-Za-z0-9_@]+$";//字母数字_ @

        Pattern p = Pattern.compile(yinshuzi);

        return p.matcher(str).matches();

    }

验证各种

 /**
     * 中国电信号码格式验证 手机段: 133,153,180,181,189,177,1700,173
     * **/
    private static final String CHINA_TELECOM_PATTERN = "(^1(33|53|7[37]|8[019])\\d{8}$)|(^1700\\d{7}$)";

    /**
     * 中国联通号码格式验证 手机段:130,131,132,155,156,185,186,145,176,1707,1708,1709
     * **/
    private static final String CHINA_UNICOM_PATTERN = "(^1(3[0-2]|4[5]|5[56]|7[6]|8[56])\\d{8}$)|(^170[7-9]\\d{7}$)";

    /**
     * 中国移动号码格式验证
     * 手机段:134,135,136,137,138,139,150,151,152,157,158,159,182,183,184
     * ,187,188,147,178,1705
     *
     **/
    private static final String CHINA_MOBILE_PATTERN = "(^1(3[4-9]|4[7]|5[0-27-9]|7[8]|8[2-478])\\d{8}$)|(^1705\\d{7}$)";
    /**
     * 仅手机号格式校验
     */
    private static final String PHONE_PATTERN=new StringBuilder(300).append(CHINA_MOBILE_PATTERN)
            .append("|")
            .append(CHINA_TELECOM_PATTERN)
            .append("|")
            .append(CHINA_UNICOM_PATTERN)
            .toString();

    /**
     * 仅手机号码校验
     * @param input
     * @return
     */
    public static boolean isPhone(String input){
        return match(PHONE_PATTERN, input);
    }

    /**
     * 匹配函数
     * @param regex
     * @param input
     * @return
     */
    private static boolean match(String regex, String input) {
        return Pattern.matches(regex, input);
    }

你可能感兴趣的:(android,studio)