Open In App

Matcher Class in Java

Last Updated : 16 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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());
        }
    }
}


Output

Match 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());
    }
}


Output

Current REGEX: geek
Current INPUT: geeksforgeeks
lookingAt(): true
matches(): false


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads