Java学习总结--023正则表达式的判断校验功能

String类的功能:public boolean matches(String regex)

1.校验qq号码

public class MyTest {
    public static void main(String[] args) {
        //正则表达式:正确规则的表达式,他是一个独立的语法,很多语言都支持,他的作用就是用来校验,一段数据符不符合我定义的规则
        //案例演示
        //需求:校验qq号码.
        //1:要求必须是5 - 15 位数字
        //2:0 不能开头

        //非正则表达式实现
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你的QQ号码");
        String qqNum = sc.nextLine();
       // boolean b = checkQQNum(qqNum);
        boolean b = checkQQNum2(qqNum);
        if (b) {
            System.out.println("QQ号码规则正确");
        } else {
            System.out.println("QQ号码规则有误");
        }
    }
    private static boolean checkQQNum2(String qqNum) {
        //定义一个正则表达式
        String regx="[1-9][0-9]{4,14}";
        boolean matches = qqNum.matches(regx);
        return matches;
    }
    private static boolean checkQQNum(String qqNum) {
        //定义一个标记
        boolean flag = false;
        //校验位数
        if (qqNum.length() >= 5 && qqNum.length() <= 15 && !qqNum.startsWith("0")) {
            //下来校验每个位数是否是数字
            for (int i = 0; i < qqNum.length(); i++) {
                char ch = qqNum.charAt(i);
                if (ch >= '0' && ch <= '9') {
                    flag = true;
                } else {
                    flag = false;
                    break;//终止循环
                }
            }
        } else {
            flag = false;
        }
        return flag;
    }
}

2.判断手机号码是否满足规则

public class MyTest2 {
    public static void main(String[] args) {
        //手机号码,长度11位  以 1开头
        //手机号码每一位都是数字
        // 第二位 3 5 6 7 8 9  13  9 9887 0987
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你的手机号码");
        String phoneNum = sc.nextLine();
        boolean b = checkPhoneNum(phoneNum);
        System.out.println(b);
        if (b) {
            System.out.println("规则正确");
        } else {
            System.out.println("规则不正确");
        }
    }
    private static boolean checkPhoneNum(String phoneNum) {
        //定义标记
        boolean flag = false;
        if (phoneNum.length() == 11 & (phoneNum.startsWith("13") || phoneNum.startsWith("15") || phoneNum.startsWith("16") || phoneNum.startsWith("17") || phoneNum.startsWith("18") || phoneNum.startsWith("19"))) {
            for (int i = 2; i < phoneNum.length(); i++) {
                char ch = phoneNum.charAt(i);
                //static boolean isDigit ( char ch)
                //确定指定字符是否为数字。
                if (Character.isDigit(ch)) {
                    flag = true;
                } else {
                    flag = false;
                    break;
                }
            }
        } else {
            flag = false;
        }
        return flag;
    }
}

3.校验用户邮箱是否满足要求

public class MyTest2 {
    public static void main(String[] args) {
        //正则表达式的判断功能
        //String类的功能:public boolean matches (String regex)

        //需求:校验邮箱
        /*
         * [email protected]
         * [email protected]
         *  [email protected]
         *   [email protected]
         *   [email protected]
         * */
        // 6 ~18 个字符,可使用字母、数字、下划线,需以字母开头
        String emailRegx = "[a-zA-Z]\\w{5,17}@[1-9a-z]{3,16}\\.(com|cn|net|org)";
        boolean matches = "[email protected]".matches(emailRegx);
        System.out.println(matches);
    }
}

猜你喜欢

转载自blog.csdn.net/web116629/article/details/89945016