Java-正则篇

正则其实是一个设计模式,叫解释器模式
给定一个语言,定义它的文法的一种表示,并定义一个解释器,这个解释器使用该表示来解释语言中的句子!
正则就是定义了一个语法,然后它有一个配套的解释器,去解释该语法,生成对应的代码。

1:正则表达式
(1)就是符合一定规则的字符串
(2)常见规则
A:字符
x 字符 x。举例:‘a’表示字符a
\ 反斜线字符。
\n 新行(换行)符 (’\u000A’)
\r 回车符 (’\u000D’)

B:字符类
[abc] a、b 或 c(简单类)
[^abc] 任何字符,除了 a、b 或 c(否定)
[a-zA-Z] a到 z 或 A到 Z,两头的字母包括在内(范围)
[0-9] 0到9的字符都包括

C:预定义字符类
. 任何字符。我的就是.字符本身,怎么表示呢? .
\d 数字:[0-9]
\w 单词字符:[a-zA-Z_0-9]
在正则表达式里面组成单词的东西必须有这些东西组成

D:边界匹配器
^ 行的开头
$ 行的结尾
\b 单词边界
就是不是单词字符的地方。
举例:hello world?haha;xixi

E:Greedy 数量词
X? X,一次或一次也没有
X* X,零次或多次
X+ X,一次或多次
X{n} X,恰好 n 次
X{n,} X,至少 n 次
X{n,m} X,至少 n 次,但是不超过 m 次
(3)常见功能:
A:判断功能
String类的public boolean matches(String regex)
B:分割功能
String类的public String[] split(String regex) 根据给定正则表达式的匹配拆分此字符串。
C:替换功能
String类的public String replaceAll(String regex,String replacement)
D:获取功能
Pattern和Matcher
Pattern p = Pattern.compile(“a*b”);
Matcher m = p.matcher(“aaaaab”);

find():查找存不存在
group():获取刚才查找过的数据

(4)案例
【大多数公司都封装了需要的正则,例如验证手机号,邮箱,身份证号等等,所以需要用的时候直接调用,如果没有可以百度查询哦,很方便的。】

A:判断电话号码和邮箱

/* 需求:
 *         判断手机号码是否满足要求?
 *
 * 分析:
 *         A:键盘录入手机号码
 *         B:定义手机号码的规则
 *             13436975980
 *             13688886868
 *             13866668888
 *             13456789012
 *             13123456789
 *             18912345678
 *             18886867878
 *             18638833883
 *         C:调用功能,判断即可
 *         D:输出结果
 */
public class RegexDemo {
    public static void main(String[] args) {
        //键盘录入手机号码
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你的手机号码:");
        String phone = sc.nextLine();

        //定义手机号码的规则
        String regex = "1[38]\\d{9}";

        //调用功能,判断即可
        boolean flag = phone.matches(regex);

        //输出结果
        System.out.println("flag:"+flag);
    }
}



/*
 * 校验邮箱
 *
 * 分析:
 *         A:键盘录入邮箱
 *         B:定义邮箱的规则
 *             [email protected]
 *             [email protected]
 *             [email protected]
 *             [email protected]
 *             [email protected]
 *         C:调用功能,判断即可
 *         D:输出结果
 */
public class RegexTest {
    public static void main(String[] args) {
        //键盘录入邮箱
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入邮箱:");
        String email = sc.nextLine();

        //定义邮箱的规则
        //String regex = "[a-zA-Z_0-9]+@[a-zA-Z_0-9]{2,6}(\\.[a-zA-Z_0-9]{2,3})+";
        String regex = "\\w+@\\w{2,6}(\\.\\w{2,3})+";

        //调用功能,判断即可
        boolean flag = email.matches(regex);

        //输出结果
        System.out.println("flag:"+flag);
    }
}

B:按照不同的规则分割数据

/* 举例:
 *         百合网,世纪佳缘,珍爱网,QQ
 *         搜索好友
 *             性别:女
 *             范围:"18-24"
 *
 *         age>=18 && age<=24
 */
public class RegexDemo {
    public static void main(String[] args) {
        //定义一个年龄搜索范围
        String ages = "18-24";

        //定义规则
        String regex = "-";

        //调用方法
        String[] strArray = ages.split(regex);

//        //遍历
//        for(int x=0; x<strArray.length; x++){
//            System.out.println(strArray[x]);
//        }

        //如何得到int类型的呢?
        int startAge = Integer.parseInt(strArray[0]);
        int endAge = Integer.parseInt(strArray[1]);

        //键盘录入年龄
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你的年龄:");
        int age = sc.nextInt();

        if(age>=startAge && age<=endAge) {
            System.out.println("你就是我想找的");
        }else {
            System.out.println("不符合我的要求,gun");
        }
    }
}



package cn.itcast_03;

import java.util.Arrays;

/*
 * 我有如下一个字符串:"91 27 46 38 50"
 * 请写代码实现最终输出结果是:"27 38 46 50 91"
 *
 * 分析:
 *         A:定义一个字符串
 *         B:把字符串进行分割,得到一个字符串数组
 *         C:把字符串数组变换成int数组
 *         D:对int数组排序
 *         E:把排序后的int数组在组装成一个字符串
 *         F:输出字符串
 */
public class RegexTest {
    public static void main(String[] args) {
        // 定义一个字符串
        String s = "91 27 46 38 50";

        // 把字符串进行分割,得到一个字符串数组
        String[] strArray = s.split(" ");

        // 把字符串数组变换成int数组
        int[] arr = new int[strArray.length];

        for (int x = 0; x < arr.length; x++) {
            arr[x] = Integer.parseInt(strArray[x]);
        }

        // 对int数组排序
        Arrays.sort(arr);

        // 把排序后的int数组在组装成一个字符串
        StringBuilder sb = new StringBuilder();
        for (int x = 0; x < arr.length; x++) {
            sb.append(arr[x]).append(" ");
        }
        //转化为字符串
        String result = sb.toString().trim();

        //输出字符串
        System.out.println("result:"+result);
    }
}

C:把论坛中的数字替换为*

package cn.itcast_04;

/*
 * 替换功能
 *      String类的public String replaceAll(String regex,String replacement)
 *      使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。
 */
public class RegexDemo {
    public static void main(String[] args) {
        // 定义一个字符串
        String s = "helloqq12345worldkh622112345678java";

        // 我要去除所有的数字,用*给替换掉
        // String regex = "\\d+";
        // String regex = "\\d";
        //String ss = "*";


        // 直接把数字干掉
        String regex = "\\d+";
        String ss = "";

        String result = s.replaceAll(regex, ss);
        System.out.println(result);
    }
}

D:获取字符串中由3个字符组成的单词

package cn.itcast_05;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/*
 * 获取功能:
 * 获取下面这个字符串中由三个字符组成的单词
 * da jia ting wo shuo,jin tian yao xia yu,bu shang wan zi xi,gao xing bu?
 */
public class RegexDemo2 {
    public static void main(String[] args) {
        // 定义字符串
        String s = "da jia ting wo shuo,jin tian yao xia yu,bu shang wan zi xi,gao xing bu?";
        // 规则
        String regex = "\\b\\w{3}\\b";

        // 把规则编译成模式对象
        Pattern p = Pattern.compile(regex);
        // 通过模式对象得到匹配器对象
        Matcher m = p.matcher(s);
        while (m.find()) {
            System.out.println(m.group());
        }

        // 注意:一定要先find(),然后才能group()
    }
}

猜你喜欢

转载自blog.csdn.net/Smile_Sunny521/article/details/89632926