“正则表达式 Matcher 类”的版本间的差异
跳到导航
跳到搜索
Jihongchang(讨论 | 贡献) |
Jihongchang(讨论 | 贡献) |
||
第80行: | 第80行: | ||
− | </syntaxhighlight> | + | </syntaxhighlight>start 和 end 返回的是每一次匹配后 substring 的 fromIndex include 和 endIndex exclude |
2022年11月19日 (六) 11:15的版本
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() | 尝试将整个区域与模式匹配 |
matcher.start() 和 matcher.end()
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