Java Matcher matches() Method
Example
Check if two texts match the pattern "w3schools" from start to end:
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 matcher1 = pattern.matcher("W3Schools");
Matcher matcher2 = pattern.matcher("Visit W3Schools!");
System.out.println(matcher1.matches());
System.out.println(matcher2.matches());
}
}
Definition and Usage
The matches() method checks if the whole text matches the pattern.
This is different from find(), which looks for the pattern anywhere in the text. In the example, the second text contains W3Schools, but it also has other characters, so matches() returns false.
Tip: To check only one text, you can use the static Pattern.matches() method.
Syntax
public boolean matches()
Parameter Values
None.
Technical Details
| Returns: | true if the whole text matches the pattern, otherwise false. |
|---|
Related Pages
❮ RegEx Methods