Pattern.compile() / Matcher.matches() / find()
Classes for compiling regular expressions and performing pattern matching. Use Pattern.compile() to compile a regular expression, then call matcher() to create a Matcher object and check it against a string.
Syntax
// Compile a regular expression and create a Pattern object. Pattern p = Pattern.compile(String regex); Pattern p = Pattern.compile(String regex, int flags); // Create a Matcher object for the given string. Matcher m = p.matcher(CharSequence input); // Check whether the entire string matches the pattern. boolean result = m.matches(); // Search for the next substring that matches the pattern. boolean found = m.find(); // Get the string matched by the most recent find() or matches() call. String matched = m.group(); String matched = m.group(int group);
Method List
| Method | Description |
|---|---|
| Pattern.compile(String regex) | Compiles the regular expression and returns a Pattern object. |
| Pattern.compile(String regex, int flags) | Compiles the regular expression with the specified flags (e.g., Pattern.CASE_INSENSITIVE). |
| p.matcher(CharSequence input) | Returns a Matcher object that matches the pattern against the given string. |
| m.matches() | Returns true if the entire string matches the pattern. |
| m.find() | Searches for the next substring that matches the pattern. Can be called repeatedly to find successive matches. |
| m.group() | Returns the string matched by the most recent match operation. |
| m.group(int group) | Returns the string captured by the specified capture group number. |
| m.start() / m.end() | Returns the start and end index of the matched substring. |
Sample Code
import java.util.regex.*;
// Use matches() to check whether a string is in postal code format.
Pattern p = Pattern.compile("\\d{3}-\\d{4}");
Matcher m = p.matcher("123-4567");
System.out.println(m.matches()); // Prints 『true』.
// Use find() to extract all email addresses from a string.
String text = "Contact: alice@example.com or bob@test.jp.";
Pattern emailPattern = Pattern.compile("[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}");
Matcher emailMatcher = emailPattern.matcher(text);
while (emailMatcher.find()) {
System.out.println(emailMatcher.group()); // Prints 『alice@example.com』 then 『bob@test.jp』.
}
// Use group() to extract capture groups.
Pattern datePattern = Pattern.compile("(\\d{4})-(\\d{2})-(\\d{2})");
Matcher dateMatcher = datePattern.matcher("Today is 2026-03-05.");
if (dateMatcher.find()) {
System.out.println(dateMatcher.group(1)); // Prints 『2026』.
System.out.println(dateMatcher.group(2)); // Prints 『03』.
System.out.println(dateMatcher.group(3)); // Prints 『05』.
}
// Use the CASE_INSENSITIVE flag to ignore case.
Pattern casePattern = Pattern.compile("java", Pattern.CASE_INSENSITIVE);
Matcher caseMatcher = casePattern.matcher("Java is a general-purpose language.");
System.out.println(caseMatcher.find()); // Prints 『true』.
Notes
Pattern.compile() compiles a regular expression string into a reusable Pattern object. When the same pattern is used repeatedly, reusing a compiled Pattern improves performance.
matches() checks whether the entire string matches the pattern, while find() searches for matching substrings one at a time. matches() requires a full-string match, whereas find() performs partial matching. Choose the one that fits your intent.
To replace matches using a regular expression, use Matcher.replaceAll() / replaceFirst().
If you find any errors or copyright issues, please contact us.