Java 中的 MatchResult end() 方法示例。
javaobject oriented programmingprogramming更新于 2025/2/12 0:12:17
java.util.regex.MatcheResult 接口提供了检索匹配结果的方法。
您可以使用 Matcher 类的 toMatchResult() 方法获取此接口的对象。此方法返回表示当前匹配器匹配状态的 MatchResult 对象。
此接口的 end() 方法返回上次匹配发生后的偏移量。
示例
import java.util.Scanner;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main( String args[] ) {
String regex = "you$";
//读取用户输入
Scanner sc = new Scanner(System.in);
String input = "Hello how are you";
//实例化 Pattern 类
Pattern pattern = Pattern.compile(regex);
//实例化 Matcher 类
Matcher matcher = pattern.matcher(input);
//验证是否发生匹配
if(matcher.find()) {
System.out.println("Match found");
}
MatchResult res = matcher.toMatchResult();
int end = res.end();
System.out.println(end);
}
}
输出
输入文本: hello how are you 找到匹配项 17
相关文章
有用资源
java 参考教程 - 该教程包含有关 java 的更多信息:https://www.cainiaomax.com/java/

