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");
}
}
}
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.
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");
}
}
}
OutputSubsequence 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.
Similar Reads
Java Reader Class Reader class in Java is an abstract class used for reading character streams. It serves as the base class for various subclasses like FileReader, BufferedReader, CharArrayReader, and others, which provide more efficient implementations of the read() method. To work with the Reader class, we must ext
6 min read
Static class in Java Java allows a class to be defined within another class. These are called Nested Classes. Classes can be static which most developers are aware of, henceforth some classes can be made static in Java. Java supports Static Instance Variables, Static Methods, Static Block, and Static Classes. The class
3 min read
Wrapper Classes in Java A Wrapper class in Java is one whose object wraps or contains primitive data types. When we create an object in a wrapper class, it contains a field, and in this field, we can store primitive data types. In other words, we can wrap a primitive value into a wrapper class object. Let's check on the wr
6 min read
Nested Classes in Java In Java, it is possible to define a class within another class, such classes are known as nested classes. They enable you to logically group classes that are only used in one place, thus this increases the use of encapsulation and creates more readable and maintainable code. The scope of a nested cl
5 min read
Object Class in Java Object class in Java is present in java.lang package. Every class in Java is directly or indirectly derived from the Object class. If a class does not extend any other class then it is a direct child class of the Java Object class and if it extends another class then it is indirectly derived. The Ob
7 min read