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$";
//从用户读取输入
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);
}
}输出结果
Enter input text: hello how are you Match found 17
热门推荐
10 儿媳买车妈妈祝福语简短
11 生日祝福语 简短独特高考
12 送个姐妹生日祝福语简短
13 恭贺新婚文案祝福语简短
14 炸鸡拼盘生日祝福语简短
15 祝女性生日祝福语简短
16 顾客简短祝福语大全集
17 简短朋友旅行祝福语大全
18 国庆求婚祝福语简短精辟