Matcher find() method in Java with Examples

Last Updated : 3 Jul, 2026

The find() method of the Matcher class is used to search for the next occurrence of a pattern in the input string. It returns true if a matching subsequence is found; otherwise, it returns false.

  • Returns a boolean value (true or false).
  • Does not require any parameters.
  • Can be called repeatedly to find multiple matches.
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("Pattern Found");
        } else {
            System.out.println("Pattern Not Found");
        }
    }
}

Output
Pattern Found

Explanation: The find() method searches the string for the word "Java". Since the pattern exists in the input string, it returns true and prints "Pattern Found".

Syntax

public boolean find()

Returns:

  • true -> if a matching subsequence is found.
  • false -> if no match is found.

Example 1: Java code to illustrate find() method

Java
import java.util.regex.*;

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

        // Get the regex to be checked
        String regex = "Geeks";

        // 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 subsequence
        // using find() method
        System.out.println(matcher.find());
    }
}

Output
true

Explanation: The pattern "Geeks" is present in the string "GeeksForGeeks", so find() successfully locates the first occurrence and returns true.

Example 2:

Java
import java.util.regex.*;

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

        // Get the regex to be checked
        String regex = "GFG";

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

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

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

        // Get the subsequence
        // using find() method
        System.out.println(matcher.find());
    }
}

Output
true

Explanation: The pattern "GFG" appears in the given string multiple times. The find() method detects the first occurrence and returns true.

Comment