1 正则表达式的概述和简单使用

  • 正则表达式:正确规则的表达式 规则java给我们定的
    是指一个用来描述或者匹配一系列符合某个句法规则的字符串的单个字符串。其实就是一种规则。有自己特殊的应用。
  • 案例演示
    需求:校验手机号的规则:
    11位 1开头 第二位3 5 6 7 8 9 后面9位任意数字

非正则表达式的实现

package demo1;

import java.util.Scanner;

public class Demo1 {
    public static void main(String[] args) {
        //校验手机号的规则:
        //11位 1开头 第二位3 5 6 7 8 9 后面9位任意数字
        Scanner sc = new Scanner(System.in);
        System.out.println("输入手机号");
        String number = sc.nextLine();
        boolean b = checknumber(number);
        if(b){
            System.out.println("手机号规则正确");
        }else{
            System.out.println("手机号规则错误");
        }
    }

    private static boolean checknumber(String number) {
        if(number.length()==11&&number.startsWith("1")&&number.charAt(1)=='3'||number.charAt(1)=='5'||number.charAt(1)=='6'||number.charAt(1)=='7'||number.charAt(1)=='8'||number.charAt(1)=='9'){//
            //if(number.length()==11&&number.startsWith("13")||number.startsWith("15")||number.startsWith("16")||number.startsWith("17")||number.startsWith("18")||number.startsWith("19"))
            for (int i = 2; i < number.length(); i++) {
                char ch = number.charAt(i);
                if(ch>='0'&&ch<='9'){
                    return true;
                }
            }
        }
        return false;
    }
}

正则表达式的实现

package demo1;

import java.util.Scanner;

public class Demo2 {
    public static void main(String[] args) {
        //校验手机号的规则:
        //11位 1开头 第二位3 5 6 7 8 9 后面9位任意数字
        String Phoneregx="[1][3,5,6,7,8,9][0-9]{9}";
        Scanner sc = new Scanner(System.in);
        System.out.println("输入手机号");
        String number = sc.nextLine();
        boolean b = number.matches(Phoneregx);
        if(b){
            System.out.println("手机号规则正确");
        }else{
            System.out.println("手机号规则错误");
        }
    }
}

2 正则表达式的组成规则
规则字符在java.util.regex Pattern类中

  • 字符
    x 字符 x。举例:‘a’表示字符a
    \ 反斜线字符。
    \n 新行(换行)符 (’\u000A’)
    \r 回车符 (’\u000D’)
  • 字符类
    [abc] a、b 或 c(简单类)
    [^abc] 任何字符,除了 a、b 或 c(否定)
    [a-zA-Z] a到 z 或 A到 Z,两头的字母包括在内(范围)
    [0-9] 0到9的字符都包括
  • 预定义字符类
    . 任何字符。我的就是.字符本身,怎么表示呢? .
    \d 数字:[0-9]
    \w 单词字符:[a-zA-Z_0-9]
    在正则表达式里面组成单词的东西必须有这些东西组成
    \s 匹配空格字符
  • 边界匹配器
    ^ 行的开头
    $ 行的结尾
    \b 单词边界
    就是不是单词字符的地方。
    举例:hello world?haha;xixi
  • Greedy 数量词
    X? X,一次或一次也没有 比如""空串 就是没有
    X* X,零次或多次 大于等于1次 都算多次
    X+ X,一次或多次
    X{n} X,恰好 n 次
    X{n,} X,至少 n 次
    X{n,m} X,至少 n 次,但是不超过 m 次

案例演示

public class MyTest {
    public static void main(String[] args) {
        //正则表达式:正确规则的表达式,是一种独立的语法,很多语言都支持。他的作用,就是用来校验数据符不符合我定义的规则。
        //正则表达式:定义规则的一种语法
        //定义字符串,字符串的值,可以是正则表达式
        String regx = "a";
        regx = "abc";
        //是我列表中的某个
        regx = "[a,b,c]";
        //不是我列表中的某个
        regx = "[^a,b,c]";
        //不是数字字符
        regx = "[^0,1,2,3,4,5,6,7,8,9]";
        regx = "[a-z]";
        regx = "[A-Z]";
        regx = "[0-9]";
        regx = "[a-zA-Z0-9_]";
        regx = "\\w"; //跟[a-zA-Z0-9_]意思一样
        regx = "\\W"; //跟[^a-zA-Z0-9_]意思一样
        regx = "\\d"; // 跟 [0-9] 意思一样
        regx = "\\D"; //跟 [^0-9] 意思一样
        regx = "."; //匹配单个任意字符
        regx = "\\."; //转义 . 让.只表示.
        regx = ".."; //匹配两个任意字符
        // | 在正则表达式里面代表的式或者的意思
        regx = "\\|";
        regx = "a|b|c";
        regx = "\\\\";
        regx = "^abc$";
        //+ 一个或多个
        regx = "[0-9]+";
        regx = "[0-9a-zA-Z]+";
        //零次或多次  大于等于1次 都算多次
        regx = "[a-z]*";
        //0次或1次
        regx = "[A-Z]?";
        //{n} 正好n个
        regx = "\\w{6}";
        //{n,} 至少n个
        regx = "\\d{6,}";
        //{n,m} 至少n个最多m个
        regx = "\\d{6,18}";
        regx = " ";
        regx = "\\s"; //空格
        regx = "\\s+"; //多个空格
        regx = "\\S"; // 不是空格

        ///* 1:要求必须是5 - 15 位数字
        //         2:0 不能开头*/
        regx = "[1-9][0-9]{4,14}";

        //判断一个字符串符不符合正则表达式定义的规则
        boolean b = "02252524aaaa14".matches(regx);
        System.out.println(b);

    }
}

正则表达式的判断功能

  • 正则表达式的判断功能
    String类的功能:public boolean matches(String regex)

案例演示

  • 校验网易邮箱 xxx@x@163.com
    6~18个字符,可使用字母、数字、下划线,需要以字母开头

非正则表达式的实现

public class MyTest3 {
    public static void main(String[] args) {
        //校验网易邮箱 xxx@x@163.com
        //6~18个字符,可使用字母、数字、下划线,需要以字母开头
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你的网易邮箱");
        String eMail = sc.nextLine();
        boolean b = checkMail(eMail);

        if (b) {
            System.out.println("规则正确");
        } else {
            System.out.println("规则错误");
        }
    }

    private static boolean checkMail(String eMail) {
        if (!eMail.endsWith("@163.com")) {
            return false;
        }
        boolean flag = false;
        String mail = eMail.substring(0, eMail.lastIndexOf("@"));
        System.out.println(mail);
        if (mail.length() >= 6 && mail.length() <= 18 && Character.isLetter(mail.charAt(0))) {
            for (int i = 1; i < mail.length(); i++) {
                char ch = mail.charAt(i);
                if (Character.isDigit(ch) || Character.isLetter(ch) || ch == '_') {
                    flag = true;
                } else {
                    return false;
                }
            }
        }
        return flag;
    }
}

正则表达式的实现

package demo2;

import java.util.Scanner;

public class Demo1 {
    public static void main(String[] args) {
        //网易邮箱的正则
        //校验网易邮箱 xxx@x@163.com
        //6~18个字符,可使用字母、数字、下划线,需要以字母开头
        String s = "[a-zA-Z]\\w{5,17}@163\\.com";
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入网易邮箱号");
        String mail = sc.nextLine();
        boolean d = mail.matches(s);
        if(d){
            System.out.println("邮箱规则正确");
        }else{
            System.out.println("邮箱规则错误");
        }
    }
}