正则表达式 Matcher 类

来自姬鸿昌的知识库
Jihongchang讨论 | 贡献2022年11月19日 (六) 12:08的版本 →‎matcher.matches() 整体匹配
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳到导航 跳到搜索

https://www.bilibili.com/video/BV1Eq4y1E79W/?p=19

Matcher 对象是对输入字符串进行解释和匹配的引擎。

与 Pattern 类一样,Matcher 也没有公共构造方法。

需要调用 Pattern 对象的 matcher 方法来获得1个 Matcher 对象。



方法一览

方法 说明
public int start() 返回以前匹配的初始索引
public int start(int group) 返回在以前的匹配操作期间,由给定组所捕获的子序列的初始索引
public int end() 返回最后匹配字符之后的偏移量
public int end(int group) 返回在以前的匹配操作期间,由给定组所捕获子序列的最后字符之后的偏移量
public boolean lookingAt() 尝试将从区域开头开始的输入序列与该模式匹配
public boolean find() 尝试查找与该模式匹配的输入序列的下一个子序列
public boolean find(int start) 重置此匹配器,然后尝试查找匹配该模式、从指定索引开始的输入序列的下一个子序列
public boolean matches() 尝试将整个区域与模式匹配
public Matcher appendReplacement(StringBuffer sb, String replacement) 实现非终端添加和替换步骤
public StringBuffer appendTail(StringBuffer sb) 实现终端添加和替换步骤
public String replaceAll(String replacement) 替换模式与给定替换字符串相匹配的输入序列的每个子序列
public String replaceFirst(String replacement) 替换模式与给定替换字符串匹配的输入序列的第一个子序列
public static String quoteReplacement(String s) 返回指定字符串的字面替换字符串。

这个方法返回一个字符串,就像传递给 Matcher 类的 appendReplacement 方法一个字面字符串一样工作

public Matcher appendReplacement(StringBuffer sb, String replacement) 实现非终端添加和替换步骤
public StringBuffer appendTail(StringBuffer sb) 实现终端添加和替换步骤


matcher.start() 和 matcher.end()

示例1:

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

public class MatcherMethod {

    public static void main(String[] args) {

        String content = "hello edu jack tom hello smith hello";

        String regStr = "hello";

        Pattern pattern = Pattern.compile(regStr);

        Matcher matcher = pattern.matcher(content);

        while (matcher.find()) {
            System.out.println("==============================");
            System.out.println("matcher.start():" + matcher.start());
            System.out.println("matcher.end():" + matcher.end());
        }

    }

}
==============================
matcher.start():0
matcher.end():5
==============================
matcher.start():19
matcher.end():24
==============================
matcher.start():31
matcher.end():36

start 和 end 返回的是每一次匹配后 substring 的 fromIndex include 和 endIndex exclude



示例2:

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

public class MatcherMethod {

    public static void main(String[] args) {

        String content = "hello edu jack tom hello smith hello";

        String regStr = "hello";

        Pattern pattern = Pattern.compile(regStr);

        Matcher matcher = pattern.matcher(content);

        while (matcher.find()) {
            System.out.println("==============================");
            System.out.println("matcher.start():" + matcher.start());
            System.out.println("matcher.end():" + matcher.end());
            System.out.println("找到:" + content.substring(matcher.start(), matcher.end()));
        }

    }

}
==============================
matcher.start():0
matcher.end():5
找到:hello
==============================
matcher.start():19
matcher.end():24
找到:hello
==============================
matcher.start():31
matcher.end():36
找到:hello



matcher.matches() 整体匹配

整体匹配方法,常用于校验某个字符串是否满足某个规则

示例1:

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

public class MatcherMethod {

    public static void main(String[] args) {

        String content = "hello edu jack tom hello smith hello";

        String regStr = "hello";

        Pattern pattern = Pattern.compile(regStr);

        Matcher matcher = pattern.matcher(content);

        //整体匹配方法,常用于校验某个字符串是否满足某个规则
        System.out.println("matcher.matches():" + matcher.matches());

    }

}
matcher.matches():false


示例2:

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

public class MatcherMethod {

    public static void main(String[] args) {

        String content = "hello edu jack tom hello smith hello";

        String regStr = "hello.*";

        Pattern pattern = Pattern.compile(regStr);

        Matcher matcher = pattern.matcher(content);

        //整体匹配方法,常用于校验某个字符串是否满足某个规则
        System.out.println("matcher.matches():" + matcher.matches());

    }

}
matcher.matches():true



matcher.replaceAll(String replacement) 正则表达式匹配替换

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

public class MatcherMethod {

    public static void main(String[] args) {

        String content = "hello edu jack hspedutom hello smith hello hspedu hspedu";

        String regStr = "hspedu";

        Pattern pattern = Pattern.compile(regStr);

        Matcher matcher = pattern.matcher(content);

        //完成如果 content 有 hspedu 就替换成 韩顺平教育
        String newContent = matcher.replaceAll("韩顺平教育");

        //注意:返回的字符串才是替换后的字符串
        System.out.println("content:" + content);

        System.out.println("newContent:" + newContent);

    }

}
content:hello edu jack hspedutom hello smith hello hspedu hspedu
newContent:hello edu jack 韩顺平教育tom hello smith hello 韩顺平教育 韩顺平教育