Matcher start() method in Java with Examples

JavaObject Oriented ProgrammingProgramming

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.


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.


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.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();


Enter input text:
Hello how are you welcome to Tutorialspoint

Since the character t occurred thrice in the input string, you can observe three index values (representing the index of each character).

Published on 19-Nov-2019 08:09:05