Java 中 MatchResult end() 方法附带示例。
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$";
//Reading input from user
Scanner sc = new Scanner(System.in);
String input = "Hello how are you";
//Instantiating the Pattern class
Pattern pattern = Pattern.compile(regex);
//Instantiating the Matcher class
Matcher matcher = pattern.matcher(input);
//verifying whether a match occurred
if(matcher.find()) {
System.out.println("Match found");
}
MatchResult res = matcher.toMatchResult();
int end = res.end();
System.out.println(end);
}
}输出
Enter input text: hello how are you Match found 17
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP