In Java, Matcher is a class that is implemented by the MatchResult interface, that performs match operations on a character sequence by interpreting a Pattern.
Below, we can see the declaration of java.util.regex.Matcher in java.lang.Object Class:
public final class Matcher extends Object implements MatchResult
By invoking the pattern's matcher method, a matcher is created from a pattern. If a matcher is created once, we can perform three different kinds of match operations on it:
- matches(): Try to match the total input sequence against the pattern.
- lookingAt(): Try to match the input sequence, against the pattern, starting at the beginning.
- find(): This scans the input sequence and looks for the next subsequence especially matches the pattern.
Methods of Matcher class:
Below the methods of the Matcher class are grouped in the table for convenience according to their functionality.
1. Index Methods:
It provides useful index values. It shows precisely whether the match was found in the input string or not:
S. No. | Method Name | Description |
---|
1 | public int start() | This method returns the start index of the previous match. |
2 | public int start(int group) | This method returns the start index of the subsequence captured by the given group during the previous match operation. |
3 | public int end() | This method returns the offset after the last character is matched. |
4 | public int end(int group) | This method returns the offset after the last character of the subsequence captured by the given group during the previous match operation. |
2. Study Methods:
It reviews the input string and returns a boolean indicating whether the pattern is found or not:
S. No. | Method Name | Description |
---|
1 | public boolean lookingAt() | This method aims to match the input sequence, starting at the beginning of the region, against the pattern. |
2 | public boolean find() | This method aims to find the next subsequence of the input sequence that matches the pattern. |
3 | public boolean find(int start) | Resets this matcher and then tries to find the next subsequence of the input sequence which matches the pattern, starting at the specified index. |
4 | public boolean matches() | This method aims to match the entire region against the pattern. |
3. Replacement Methods:
These are useful methods for replacing text in an input string:
S. No. | Method Name | Description |
---|
1 | public Matcher appendReplacement(StringBuffer sb, String replacement) | This method implements a non-terminal append-and-replace step. |
2 | public StringBuffer appendTail(StringBuffer sb) | This method implements a terminal append-and-replace step. |
3 | public String replaceAll(String replacement) | This method replaces every subsequence of the input sequence that matches the pattern with the given replacement string. |
4 | public String replaceFirst(String replacement) | This method replaces the first subsequence of the input sequence that matches the pattern with the given replacement string. |
5 | public static String quoteReplacement(String s) | This method returns a literal replacement String for the specified String, this method also produces a String which will work in the appendReplacement method as a literal replacement of the Matcher class. |
Example 1: Here we can see the example GFG.java which count the number of times the word "geek" appears in the input string using start() and end() :
Java
// Java program to demonstrate the
// methods of Matcher class in Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class GFG {
private static final String REGEX = "\\bgeek\\b";
private static final String INPUT
= "geek geek geek geekie geeks";
public static void main(String[] args)
{
Pattern pat = Pattern.compile(REGEX);
// here get a matcher object
Matcher mat = pat.matcher(INPUT);
// initialize a count variable to count
int count = 0;
// try to match the entire input sequence against
// the pattern using the loop
while (mat.find()) {
count++;
System.out.println("Match number " + count);
System.out.println("start(): " + mat.start());
System.out.println("end(): " + mat.end());
}
}
}
OutputMatch number 1
start(): 0
end(): 4
Match number 2
start(): 5
end(): 9
Match number 3
start(): 10
end(): 14
Example 2: In this example, we can see GFG.java, the lookingAt() and matches() both attempt to match an input sequence against a pattern.
Java
// Java program to demonstrate the
// methods of Matcher class in Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class GFG {
private static final String REGEX = "geek";
private static final String INPUT = "geeksforgeeks";
private static Pattern pat;
private static Matcher mat;
public static void main(String[] args)
{
// Initialization for pattern and matcher
pat = Pattern.compile(REGEX);
mat = pat.matcher(INPUT);
System.out.println("Current REGEX: " + REGEX);
System.out.println("Current INPUT: " + INPUT);
System.out.println("lookingAt(): "
+ mat.lookingAt());
System.out.println("matches(): " + mat.matches());
}
}
OutputCurrent REGEX: geek
Current INPUT: geeksforgeeks
lookingAt(): true
matches(): false
Similar Reads
Java Pattern Class
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 p
3 min read
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
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
Types of Classes in Java
A class is a blueprint in the Java programming language from which an individual object can be built. In Java, we may declare a class by using the class keyword. Class members and functions are declared simply within the class. Classes are required for the creation of Java programs. The object-orien
8 min read
Pair Class in JavaTuples
A Pair is a Tuple from JavaTuples library that deals with 2 elements. Since this Pair is a generic class, it can hold any type of value in it.Since Pair is a Tuple, hence it also has all the characteristics of JavaTuples:Â They are TypesafeThey are ImmutableThey are IterableThey are SerializableThey
5 min read
Octet Class in JavaTuples
A Octet is a Tuple from JavaTuples library that deals with 3 elements. Since this Octet is a generic class, it can hold any type of value in it.Since Octet is a Tuple, hence it also has all the characteristics of JavaTuples:Â Â They are TypesafeThey are ImmutableThey are IterableThey are Serializable
6 min read
Sextet Class in JavaTuples
A Sextet is a Tuple from JavaTuples library that deals with 3 elements. Since this Sextet is a generic class, it can hold any type of value in it.Since Sextet is a Tuple, hence it also has all the characteristics of JavaTuples:Â Â They are TypesafeThey are ImmutableThey are IterableThey are Serializa
6 min read
Java Class File
A Java class file is a file containing Java bytecode and having .class extension that can be executed by JVM. A Java class file is created by a Java compiler from .java files as a result of successful compilation. As we know, a single Java programming language source file (or we can say .java file)
5 min read
Triplet Class in JavaTuples
A Triplet is a Tuple from JavaTuples library that deals with 3 elements. Since this Triplet is a generic class, it can hold any type of value in it. Since Triplet is a Tuple, hence it also has all the characteristics of JavaTuples:Â They are TypesafeThey are ImmutableThey are IterableThey are Serial
5 min read
Method Class | equals() Method in Java
The java.lang.reflect.Method.equals(Object obj) method of Method class compares this Method Object against the specified object as parameter to equal(object obj) method. This method returns true if the Method object is the same as the passed object. Two Methods are the same if they were declared by
3 min read