Java Pattern matcher() Method
Example
Find out if the word "w3schools" is in a sentence:
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("w3schools", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher("Visit W3Schools!");
if (matcher.find()) {
System.out.println("Match found");
} else {
System.out.println("Match not found");
}
}
}
Definition and Usage
The matcher() method creates a Matcher object that searches a text for the pattern.
Use the methods of the Matcher, like find(), matches() and group(), to do the search and get the results.
Syntax
public Matcher matcher(CharSequence input)
Parameter Values
| Parameter | Description |
|---|---|
| input | Required. The text to search. It is usually a String, but any CharSequence (an object that holds text, like a StringBuilder) works. |
Technical Details
| Returns: | A new Matcher object for the text. |
|---|
Related Pages
❮ RegEx Methods