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 start() method of the Matcher class returns the starting index of the matched character.
Example
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 StartExample { 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 start = matcher.start(); System.out.println(start); } } }
Output
Enter input text: Hello how are you welcome to Tutorialspoint 26 31 42
Since the character t occurred thrice in the input string, you can observe three index values (representing the index of each character).