Java正则表达式

概述

正则表达式语法:
image

image

案例

package com.mrs;

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

/**
 * description: Regex
 * date: 2022/9/4 18:30
 * author: MR.孙
 */
public class Regex {

    public static void main(String[] args) {

        // regex1();

        //regex2(); 判断手机号是否为13或18开头

        // regex3();  //匹配手机号

        //regex4(); 统计cat出现的次数

        //regex5(); lookingAt()和mathcers方法

        //regex6();替换正则表达式的文本


        //方法讲解:https://blog.csdn.net/liushu_it/article/details/38316309
        regex7();//appendReplacement和appendTail方法来替换文本。

    }


    private static void regex7() {
        //appendReplacement和appendTail方法来替换文本。
        String input = "aabfooaabfooabfoob";
        String regex = "a*b";
        String replace = "-";

        Pattern pattern = Pattern.compile(regex);

        // get a matcher object
        Matcher matcher = pattern.matcher(input);
        StringBuffer sb = new StringBuffer();
        while(matcher.find()){
            matcher.appendReplacement(sb,replace);
            //第一次 -
            //第二次 foo-
            //第三次 foo-
            //最后一次 foo-
        }
        matcher.appendTail(sb);
        System.out.println(sb);

    }


    /**
     * @description:  获取捕获组数量和内容
     * @return: void
     * @author: MR.孙
     * @date: 2022/9/4 19:03
    */
    public static  void regex1(){
        String line = "This order was placed for QT3000! OK?";
        //正则表达式
        String pattern = "(.*)(\\d)(.*)";

        //创建Pattern对象
        Pattern r = Pattern.compile(pattern);

        //创建Mathcher对象
        Matcher m = r.matcher(line);

        if(m.find()){
            System.out.println("捕获组的数量:->"+m.groupCount());//不包含组0
            System.out.println("组0->"+m.group(0));
            System.out.println("组1->"+m.group(1));
            System.out.println("组2->"+m.group(2));
            System.out.println("组3->"+m.group(3));

        }else{
            System.out.println("没有匹配的字符串");
        }
    }

    /**
     * @description:  判断手机号是否为13或18开头
     * @return: void
     * @author: MR.孙
     * @date: 2022/9/4 19:04
    */
    public static void regex2(){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入11位手机号:");
        // 15623799711
        String phoneNumber = sc.next();

        //正则表达式
        String regex = "1[38]\\d{9}";
        //正则表达式和手机号进行匹配
        boolean res = phoneNumber.matches(regex);
        if(res){
            System.out.println("匹配成功:->"+res);
        }else{
            System.out.println("匹配失败:->"+res);
        }

    }

    public static void regex3(){
        String str = "15511259673";
        String pattern = "0?(13|14|15|17|18)[0-9]{9}";

        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(str);
        System.out.println(m.matches());
    }

    /**
     * @description:  统计cat出现的次数
     * @return: void
     * @author: MR.孙
     * @date: 2022/9/4 19:26
    */
    private static void regex4() {
       String str = "cat cat cattie cat";
       String regex = "\\bcat\\b";

        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);
        int count = 0;//统计单词出现数量
        while(matcher.find()){
            count++;
            System.out.println("匹配单词的数量为:->"+count);
            //start方法返回上一个匹配操作期间给定组捕获的子序列的起始索引,end返回匹配的最后一个字符的索引加1。
            System.out.println("start()"+ matcher.start());
            System.out.println("end()"+ matcher.end());
        }

    }



    private static void regex5() {
        String str = "fooooooooooooooooooo";
        String regex = "foo";

        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);

        System.out.println("Current input is -> " + str);
        System.out.println("Current Regex is ->" + regex);

        //lookingAt():  只要匹配到返回true
        System.out.println("lookingAt(): ->" + matcher.lookingAt());
        //mathers():    完全匹配才为true
        System.out.println("mathers(): -> " + matcher.matches());
    }


    private static void regex6() {
        //replaceFirst()和replaceAll()方法替换匹配给定正则表达式的文本。
        //正如其名称所示,replaceFirst()替换第一个匹配项,replaceAll()替换所有匹配项。

        String input = "The dog syas meow."+"All dogs say meow.";
        String regex = "dog";//正则表达式
        String replace = "cat";//替换匹配给定正则表达式的文本

        Pattern pattern = Pattern.compile(regex);

        //获取matcher对象
        Matcher matcher = pattern.matcher(input);
        input = matcher.replaceAll(replace);
        System.out.println(input);

    }



}
posted @ 2022-09-04 21:26  长情c  阅读(30)  评论(0)    收藏  举报