The matcher(CharSequence) method of the Pattern class is used to create a Matcher object for a given input sequence. The returned Matcher object is then used to perform various regular expression operations such as matches(), find(), lookingAt(), and replacement operations.
- Creates a Matcher object for the specified input text.
- Enables pattern searching, validation, and replacement operations.
- Works with any object that implements the CharSequence interface, such as String, StringBuilder, and StringBuffer.
import java.util.regex.*;
public class Geeks {
public static void main(String[] args) {
// Compile the regular expression
Pattern pattern = Pattern.compile("Java");
// Create a Matcher object
Matcher matcher = pattern.matcher("I am learning Java programming.");
// Check if the pattern exists
if (matcher.find()) {
System.out.println("Match found: " + matcher.group());
System.out.println("Starting index: " + matcher.start());
System.out.println("Ending index: " + (matcher.end() - 1));
} else {
System.out.println("No match found.");
}
}
}
Output
Match found: Java Starting index: 14 Ending index: 17
Explanation: The matcher() method creates a Matcher object for the input string. The find() method searches for the word "Java" and returns true because it is present in the text. The group(), start(), and end() methods display the matched text and its position.
Syntax
public Matcher matcher(CharSequence input)
- Parameters: This method accepts a single parameter input which represents the character sequence to be matched.
- Return Value: This method returns a new matcher for this pattern.
Program 1: Using matcher() with find()
import java.util.regex.*;
public class Geeks {
public static void main(String[] args) {
// Creating a regex pattern
Pattern pattern = Pattern.compile("ee");
// Creating a matcher for the input string
Matcher matcher = pattern.matcher("geeksforgeeks");
// Searching for matches
while (matcher.find()) {
System.out.println("Match found: "
+ matcher.group()
+ " at index "
+ matcher.start()
+ " to "
+ (matcher.end() - 1));
}
}
}
Output
Match found: ee at index 1 to 2 Match found: ee at index 9 to 10
Explanation: The matcher() method creates a Matcher object for the input string "geeksforgeeks". The find() method searches for every occurrence of "ee" and prints the matched text along with its starting and ending positions.
Program 2: No Match Found
import java.util.regex.*;
public class Geeks {
public static void main(String[] args) {
// Creating a regex pattern
Pattern pattern = Pattern.compile("welcome");
// Creating a matcher
Matcher matcher = pattern.matcher("geeksforgeeks");
if (matcher.find()) {
System.out.println("Match found");
} else {
System.out.println("No match found");
}
}
}
Output
No match found
Explanation: Since the input string "geeksforgeeks" does not contain the word "welcome", the find() method returns false, and the program prints "No match found".
Common Methods Used with matcher()
| Method | Description |
|---|---|
| matches() | Checks whether the entire input matches the pattern. |
| find() | Searches for the next occurrence of the pattern. |
| lookingAt() | Checks whether the input starts with the pattern. |
| replaceAll() | Replaces all matching substrings. |
| replaceFirst() | Replaces only the first matching substring. |
| group() | Returns the matched text. |
| start() | Returns the starting index of the current match. |
| end() | Returns the ending index (exclusive) of the current match. |
Advantages of matcher(CharSequence) Method
- Creates a reusable Matcher object for performing regex operations.
- Supports searching, validation, and replacement of text.
- Works with all CharSequence implementations.
- Provides methods to retrieve matched text and match positions.
- Used together with the Pattern class for efficient regular expression processing in Java.