The java.util.regex.Matcher class represents an engine that performs various match operations. There is no constructor for this class you can create/obtain an object of this class using the matches() method of the class java.util.regex.Pattern.
The end() method of the Matcher class returns the offset after the last match represented by the current object.
The subexpression "[...]" matches the characters specified within the braces in the input string, In the following example using this to match the character t. Here,
We have compiled the regular expression using the compile() method.
Obtained the Matcher object.
Invoked the matcher() method on each match.
Example
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class EndExample { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter input text: "); String input = sc.nextLine(); String regex = "[t]"; //Creating a pattern object Pattern pattern = Pattern.compile(regex); //Matching the compiled pattern in the String Matcher matcher = pattern.matcher(input); int count =0; while (matcher.find()) { int end = matcher.end(); System.out.println(end); } } }
Output
Enter input text: Hello how are you welcome to Tutorialspoint 27 32 43
Since the character t occurred thrice in the input string, you can observe three offset values (representing the position in the input string after each occurrence).