正则表达式

//1、校验手机号
    public static void phone(){
        String phone="1733639`2256";
        String regExp = "^1[3|4|5|7|8][0-9]{9}$";
        Pattern p=Pattern.compile(regExp);
        p.matcher(phone).matches();
        if(p.matcher(phone).matches()){
            System.out.println("此号码有效");
        }else System.out.println("此号码no效");
    }
//2、校验的是数字
    public static void shuzi(){
        String shuzi="1231413254535";
        String regExp="^[0-9]*$";
        Pattern p=Pattern.compile(regExp);
        if(p.matcher(shuzi).matches()){
        System.out.println("匹配的是数字");    
        }else{
            System.out.println("不是数字");
        }
    }

//3、校验邮箱
    public static void email(){
        String email="aa@qq.`com";
        String regExp1="^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$";
        
        String regExp="^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$";
        Pattern p=Pattern.compile(regExp);
        if(p.matcher(email).matches()){
            System.out.println("是的");
        }else System.out.println("不是的");
    }
//4、验证n位的数字 (n,m);
    public static void nShuZhi(){
        String digit="2222";
        //n位数字
        String regExp="^\d{1}$";
        //(n,m)的数字
        String regExp2="^\d{1,4}$";
        //至少n位
        String regExp3="^\d{2,}$";
        //验证零和非零开头的数字
        String regExp4="^(0|[1-9][0-9]*)$";
        //验证有两位小数的正实数
        String regExp5="^[0-9]*(.[0-9]{2})?$";
        //验证非零的正整数
        String regExp6="^+?[1-9][0-9]*$";
        //验证非零的负整数
        String regExp7="^-[1-9][0-9]*$";
        //验证非负整数(正整数 + 0)
        String regExp8="^\d+$";
        //验证非正整数(负整数 + 0)
        String regExp9="^((-\d+)|(0+))$";
        //验证长度为3的字符
        String regExp10="^.{3}$";
        Pattern p=Pattern.compile(regExp2);
        if(p.matcher(digit).matches()){
            System.out.println("是数字");
        }else
        System.out.println("不是");
    }
//5、验证由26个英文字母组成的字符串
    public static void ziMu(){
        String zi="阿爸";
        String regex="^[A-Za-z]+$";
        //验证由26个大写英文字母组成的字符串
        String regex1="^[A-Z]+$";
        //验证由数字和26个英文字母组成的字符串:
        String regex2="^[A-Za-z0-9]+$";
        //验证由数字、26个英文字母或者下划线组成的字符串
        String regex3="^\w+$";
        Pattern p=Pattern.compile(regex3);
        if(p.matcher(zi).matches()){
            System.out.println("是英文");
        }else System.out.println("不是的");
    }
//6、验证汉字:
    public static void hanZi(){
        String hanZi="得得";
        //验证汉字:
        String regex="^[u4e00-u9fa5]{2,}$";
        Pattern p=Pattern.compile(regex);
        if(p.matcher(hanZi).matches()){
            System.out.println("是汉字");
        }else System.out.println("不是的");
    }
//7、验证身份证号(15位或18位数字)
    public static void card(){
        String card="412722199303041076";
        String regex="^\d{15}|\d{18}$";
        Pattern p=Pattern.compile(regex);
        if(p.matcher(card).matches()){
            System.out.println("是身份证");
        }else System.out.println("不是的");
    }
 
原文地址:https://www.cnblogs.com/L-Y-F/p/9347150.html