Pattern Class in Java
The two classes that support Regular Expression (Regex) are as follows, Pattern Class and Matcher Class. Both these classes work together. In simple language, we can think of them as, if we want to define a pattern( regular expression) then we use the Pattern Class and if we want to match that pattern against any other sequence, then we use the Matcher Class.
As you can see in the image given below, 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.
static Pattern compile(String pattern)
Here, the pattern in the compile method is a String. This String (pattern ) is then converted into a pattern which can be now used for pattern matching by the Matcher Class. After the pattern is matched, it returns a Pattern object that contains the pattern. The Pattern object created here will now be used to create a Matcher. The Matcher is created by calling the matcher() method defined by Pattern.
Matcher matcher(CharSequence x)
Here, x is the character sequence against which the pattern will be matched. Since this is the input you are taking, it is also called the input sequence. CharSequence is an interface that provides read-only access to the set of characters.
Let us check whether the pattern matches or not: Pattern Matched
Checking for GFG in GeeksforGeeks: subsequence GFG found