Matcher lookingAt() method in Java with Examples

Last Updated : 20 Jul, 2026

The lookingAt() method of the Matcher class is used to determine whether the beginning of the input sequence matches the specified regular expression. Unlike the matches() method, lookingAt() does not require the entire input string to match the pattern. It only checks whether the match starts at the beginning of the input.

  • Can be used multiple times on the same Matcher object without recompiling the pattern.
  • Often used for parsing strings that begin with a fixed keyword or format.
  • Works with any regular expression supported by Java's Pattern class.
Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Geeks {

    public static void main(String[] args) {

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

        // Create a Matcher object
        Matcher matcher = pattern.matcher("GeeksForGeeks");

        // Check whether the input starts with the pattern
        System.out.println(matcher.lookingAt());
    }
}

Output
true

Explanation: The input string "GeeksForGeeks" begins with the word "Geeks". Since the pattern matches the beginning of the input sequence, the lookingAt() method returns true.

Syntax

public boolean lookingAt()

Parameters: This method does not accept any parameters.

Return Value

  • true if the beginning of the input sequence matches the pattern.
  • false otherwise.

Program: Pattern Matches the Beginning of the String

Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Geeks {

    public static void main(String[] args) {

        Pattern pattern = Pattern.compile("GFG");

        Matcher matcher = pattern.matcher("GFGFGFGFGFG");

        System.out.println(matcher.lookingAt());
    }
}

Output
true

Explanation: The input string starts with "GFG", which matches the regular expression. Therefore, the lookingAt() method returns true.

Program: Pattern Does Not Match the Beginning

Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Geeks {

    public static void main(String[] args) {

        Pattern pattern = Pattern.compile("Java");

        Matcher matcher = pattern.matcher("Learn Java Programming");

        System.out.println(matcher.lookingAt());
    }
}

Output
false

Explanation: Although the word "Java" appears in the input string, it does not occur at the beginning. Since lookingAt() only checks for a match starting from the first character, it returns false.

Difference Between lookingAt(), matches(), and find()

MethodDescription
lookingAt()Checks whether the beginning of the input matches the pattern.
matches()Checks whether the entire input matches the pattern.
find()Searches for the next occurrence of the pattern anywhere in the input.

Advantages of lookingAt() Method

  • Checks only the beginning of the input sequence.
  • Does not require the complete string to match.
  • Returns a boolean value for easy validation.
  • Works efficiently with Java Regular Expressions.
  • Useful for prefix-based matching and parsing tasks.
Comment