Pattern compile(String) method in Java with Examples

Last Updated : 21 Jan, 2026

Pattern.compile(String) compiles a regular expression into a reusable Pattern object for matching using Matcher. It improves performance by compiling once and throws PatternSyntaxException for invalid regex syntax.

Java
import java.util.regex.*;
class GFG {
    public static void main(String[] args) {
        Pattern pattern = Pattern.compile("\\d+");
        Matcher matcher = pattern.matcher("12345");

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

Output
true

Explanation:

  • The regular expression \\d+ represents one or more digits.
  • Pattern.compile() converts the regex into a reusable Pattern object.
  • The Matcher checks the input string against the pattern.
  • The matches() method verifies that the entire string follows the regex.

Syntax

public static Pattern compile(String regex)

  • Parameters: "regex" A string representing the regular expression to be compiled.
  • Return Value: Returns a Pattern object that represents the compiled form of the given regular expression.

Example 1: The Pattern.compile(String) method compiles a regular expression into a reusable Pattern object that is used with Matcher to perform efficient string matching operations.

Java
import java.util.regex.*;
class GFG {
    public static void main(String[] args) {
        Pattern pattern = Pattern.compile("geeks");
        Matcher matcher = pattern.matcher("geeksforgeeks");
        System.out.println(matcher.find());
    }
}

Output
true

Explanation:

  • The regular expression is compiled into a Pattern object using Pattern.compile().
  • The compiled Pattern is applied to an input string by creating a Matcher object.
  • The Matcher scans the input string based on the given regex.
  • Methods like find() or matches() are used to check for pattern occurrence or full match.
  • This approach improves performance and keeps regex logic clean and reusable.

Example 2: This program demonstrates how Pattern.compile(String) is used to validate a string against a regular expression in Java

Java
import java.util.regex.*;
class GFG {
    public static void main(String[] args) {
        String regex = "[a-zA-Z0-9]+";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher("GFG123");
        if (matcher.matches()) {
            System.out.println("Valid String");
        } else {
            System.out.println("Invalid String");
        }
    }
}

Output
Valid String

Explanation:

  • The java.util.regex package is imported to use Pattern and Matcher classes.
  • A regular expression [a-zA-Z0-9]+ is defined to allow only alphanumeric characters.
  • The regex is compiled into a Pattern object using Pattern.compile().
  • A Matcher object is created to match the pattern against the string "GFG123".
  • The matches() method checks whether the entire string follows the given pattern.
  • If the condition is true, "Valid String" is printed; otherwise, "Invalid String" is printed.

Note:

  • Pattern.compile() is a static method
  • Always use it when the same regex is used multiple times
  • Separates regex definition from matching logic
Comment