Javaregex-matchresult-end
提供:Dev Guides
java.util.regex.MatchResult.end()メソッド
説明
- java.time.MatchResult.end()*メソッドは、最後の文字が一致した後のオフセットを返します。
宣言
以下は* java.time.MatchResult.end()*メソッドの宣言です。
int end()
戻り値
最後の文字が一致した後のオフセット。
例外
- IllegalStateException -一致がまだ試行されていない場合、または前の一致操作が失敗した場合。
例
次の例は、java.time.MatchResult.end()メソッドの使用法を示しています。
package com.finddevguides;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MatchResultDemo {
private static final String REGEX = "(.*)(\\d+)(.*)";
private static final String INPUT = "This is a sample Text, 1234, with numbers in between.";
public static void main(String[] args) {
//create a pattern
Pattern pattern = Pattern.compile(REGEX);
//get a matcher object
Matcher matcher = pattern.matcher(INPUT);
if(matcher.find()) {
//get the MatchResult Object
MatchResult result = matcher.toMatchResult();
//Prints the offset after the last character matched.
System.out.println("First Capturing Group - Match String end(): "+result.end());
}
}
}
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
First Capturing Group - Match String end(): 53