Open In App

Java Pattern Class

Last Updated : 15 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The Pattern class in Java is used for defining regular expressions (regex) to perform pattern matching on strings. It is part of the java.util.regex package and it plays a key role in searching, replacing, and manipulating strings based on patterns. The Matcher class works together with Pattern to perform regex operations like finding matches in text.

Example: Basic Pattern Matching

Java
// Pattern matching using Regex
import java.util.regex.*;

public class Geeks {
  
    public static void main(String[] args) {
      
        // Creating a pattern
        Pattern p = Pattern.compile("GeeksforGeeks");

        // Creating a matcher for the input
        Matcher m = p.matcher("GeeksforGeeks");

        // Checking if the pattern matches
        boolean b = m.matches();

        // Condition check whether the pattern is matched
        if (b) {
            System.out.println("Pattern Matched");
        } else {
            System.out.println("Pattern Not Matched");
        }
    }
}

Output
Pattern Matched

Explanation: In the above example, the Pattern.compile(“GeeksforGeeks”) method compiles the string “GeeksforGeeks” into a pattern. The m.matches() checks if the entire input string exactly matches the pattern.

Pattern Class Structure

As we can see in the below image, the Pattern Class belongs to java.util.regex (package), and the package belongs to java.base (module). The pattern class defines no constructor.

PatternClassJava

So, let us see how a pattern is created. The pattern is created using the compile() factory method.

Syntax of Key Methods in Pattern Class

1. compile(String pattern): Compiles the given regex into a Pattern object, which can be used for matching operations.

static Pattern compile(String pattern)

2. matcher(CharSequence input): Creates a Matcher object for a given input string. The Matcher is used to perform matching operations like matches(), find(), and replaceAll().

Matcher matcher(CharSequence input)

Example: Searching for Substrings with the find() Method

The find() method of the Matcher class is used to search for the next subsequence that matches the pattern in the input string. The find() method can locate the pattern within a larger string.

Java
// Pattern Matching usingRegex to find 
// a subsequence in a string
import java.util.regex.*;

public class Geeks {
  
    public static void main(String[] args) {
      
        // Creating a pattern
        Pattern p = Pattern.compile("GeeksforGeeks");

        // Creating a matcher for the input
        Matcher m = p.matcher("GFG stands for GeeksforGeeks");

        // Determining if the input sequence contains the 
        // subsequence that matches the pattern
        if (m.find()) {
            System.out.println("Subsequence GFG found");
        } else {
            System.out.println("Subsequence not found");
        }
    }
}

Output
Subsequence GFG found

Explanation: In the above example, the Pattern.compile(“GeeksforGeeks”) method creates a pattern to search for “GeeksforGeeks”. The matcher.find() searches for the occurrence of the pattern in the input string and returns true if found, otherwise false. 



Next Article
Article Tags :
Practice Tags :

Similar Reads