Matcher group() method in Java with Examples

Last Updated : 3 Jul, 2026

The group() method of the Matcher class returns the substring matched by the most recent successful match operation. It is commonly used with the find() method to retrieve the actual text that matches a regular expression.

  • Must be called after a successful find() or matches() operation.
  • Returns the matched text as a String.
  • Throws IllegalStateException if no successful match exists.
Java
import java.util.regex.*;

public class Main {
    public static void main(String[] args) {

        Pattern pattern = Pattern.compile("Java");
        Matcher matcher = pattern.matcher("I am learning Java Programming.");

        if (matcher.find()) {
            System.out.println("Matched Text: " + matcher.group());
        }
    }
}

Output
Matched Text: Java

Explanation: The find() method locates the word "Java" in the input string, and group() returns the matched substring "Java".

Syntax

public String group()

Returns

  • A String containing the substring matched by the previous match operation.

Exception

  • IllegalStateException –> Thrown if no match has been attempted or the previous match failed.

Example 1: Java code to illustrate end() method

Java
import java.util.regex.*;

public class GFG {
    public static void main(String[] args)
    {

        // Get the regex to be checked
        String regex = "(G*s)";

        // Create a pattern from regex
        Pattern pattern
            = Pattern.compile(regex);

        // Get the String to be matched
        String stringToBeMatched
            = "GeeksForGeeks";

        // Create a matcher for the input String
        Matcher matcher
            = pattern
                  .matcher(stringToBeMatched);

        // Get the current matcher state
        MatchResult result
            = matcher.toMatchResult();
        System.out.println("Current Matcher: "
                           + result);

        while (matcher.find()) {
            // Get the group matched using group() method
            System.out.println(matcher.group());
        }
    }
}

Output
Current Matcher: java.util.regex.Matcher$ImmutableMatchResult@1dbd16a6
s
s

Explanation: The pattern (G*s) matches the character s in both occurrences of "Geeks". The group() method returns each matched substring, resulting in "s" being printed twice.

Example 2

Java
import java.util.regex.*;

public class GFG {
    public static void main(String[] args)
    {

        // Get the regex to be checked
        String regex = "(G*G)";

        // Create a pattern from regex
        Pattern pattern
            = Pattern.compile(regex);

        // Get the String to be matched
        String stringToBeMatched
            = "GFG FGF GFG";

        // Create a matcher for the input String
        Matcher matcher
            = pattern
                  .matcher(stringToBeMatched);

        // Get the current matcher state
        MatchResult result
            = matcher.toMatchResult();
        System.out.println("Current Matcher: "
                           + result);

        while (matcher.find()) {
            // Get the group matched using group() method
            System.out.println(matcher.group());
        }
    }
}

Output
Current Matcher: java.util.regex.Matcher$ImmutableMatchResult@1dbd16a6
G
G
G
G
G

Explanation: The pattern (G*G) matches each occurrence of G in the input string. The group() method returns every matched substring, so G is printed five times.

Advantages of Matcher.group() Method

  • Returns the exact matched text from the input string.
  • Simplifies extracting data using regular expressions.
  • Works efficiently with find() to process multiple matches.
  • Useful for text parsing, validation, and searching.
  • Eliminates the need for manual substring extraction.
Comment