Java

How to Use Pattern.matcher() Method in Java?

In Java, Pattern.matcher() is a method utilized to create a matcher object. It matches a given regular expression pattern against a specific input string. The Pattern.matcher() method is part of the Java java.util.regex package to compile a regex into a Pattern object.

This article will illustrate the Pattern.matcher() method along with numerous examples.

How to Use Pattern.matcher() Method in Java?

The matcher() method is called on the Pattern object for creating a Matcher object. It provides several types of matching operations, such as finding the next matching substring, replacing the matched substrings with a specified replacement string, and many more.

Syntax
The basic syntax of the Pattern.matcher() method in Java is:

Matcher matcher = Pattern.compile(regularExpression).matcher(inputString);

In the above code snippet,

  • The “regularExpression” is the pattern that users require to match against the input string, and the “inputString” is the string for matching the pattern.
  • The “Pattern.compile()” compiles the regex into an object, which is then utilized to create a Matcher object by calling the “matcher()” method on the Pattern object.

Here are two basic examples of using Pattern.matcher() method in Java:

Example 1: Finding a Specific Pattern in a String
Let conducted an example of finding a specific pattern using the Pattern.matcher() method in Java:

import java.util.regex.*;

public class Example {
   public static void main(String[] args) {
      String inputStr = "Hello World, it is a fox jumping";
      String patternStr = "fox";

      Pattern pattern = Pattern.compile(patternStr);
      Matcher matcher = pattern.matcher(inputStr);

      if(matcher.find()) {
         System.out.println("Found the word "" + patternStr + """);
      } else {
         System.out.println("Did not find the word "" + patternStr + """);
      }
   }
}

The description of the above code is given below:

  • The “Pattern.matcher()” method is used to create a Matcher object that is used to search for the word “fox” in the input string “Hello World, it is a fox jumping”.
  • The “find()” method is called on the Matcher object to perform the matching operation.

Output

The output of the program indicates the word “fox” is found in the terminal.

Example 2: Comparing a Pattern Using a Regular Expression
Here is an example to match a pattern via a regular expression in Java:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PatternMatcherExample {

    public static void main(String[] args) {
        String input = "Linuxhint has founded quick brown fox jumps";
        String pattern = "\\b\\w{5}\\b";
        Pattern p = Pattern.compile(pattern);
        Matcher m = p.matcher(input);
       
        while (m.find()) {
            System.out.println("Match found: " + m.group());
        }
    }
}

In this example,

  • The “matcher()” method is used to search for words in the input string that are exactly five characters long.
  • The pattern is defined using a regular expression “\\b\\w{5}\\b”, where \b matches word boundaries and \w{5} matches any five-word characters.
  • The “group()” method is utilized to retrieve the matched text. The “find()” method is utilized in a loop to search all matching patterns from users’ input.

Output

The output lists all words in the input string that are exactly five characters long.

Conclusion

In Java, the “Pattern.matcher()” method is utilized to search for patterns in each string. It returns a Matcher object that can be used to perform various operations on the string. The resulting Matcher object performs several types of matching operations on the input string, such as finding all occurrences of the pattern, replacing matched substrings with a specified replacement string, or extracting specific groups of matched substrings. This guide has covered all examples to use the “Pattern.matcher()” method in Java.

About the author

Syed Minhal Abbas

I hold a master's degree in computer science and work as an academic researcher. I am eager to read about new technologies and share them with the rest of the world.