Matches vs Find in Java Regex: Key Differences and Usage
matches() checks if the entire input string matches the pattern exactly, while find() searches for any substring within the input that matches the pattern. Use matches() for full string validation and find() to locate partial matches inside the string.Quick Comparison
This table summarizes the main differences between matches() and find() methods in Java regex.
| Aspect | matches() | find() |
|---|---|---|
| Purpose | Checks if entire string matches pattern | Searches for any substring matching pattern |
| Return Type | boolean | boolean |
| Match Scope | Whole input string | Any part of input string |
| Typical Use Case | Validation of full input | Finding occurrences inside input |
| Multiple Matches | No, single check | Yes, can find multiple matches with repeated calls |
| Reset Behavior | No need to reset | Can reset matcher to search again |
Key Differences
The matches() method attempts to match the entire input string against the regex pattern. It returns true only if the whole string fits the pattern exactly, making it ideal for validation tasks like checking if a string is a valid email or phone number.
In contrast, find() looks for the next subsequence in the input that matches the pattern. It can be called repeatedly to find multiple matches within the same string. This makes find() useful for searching and extracting parts of text, such as finding all words starting with a capital letter.
Another difference is that matches() implicitly anchors the pattern to the start and end of the string, while find() does not require this and scans through the input. Because of this, matches() is stricter, and find() is more flexible for partial matches.
Code Comparison
Here is an example using matches() to check if a string contains only digits:
public class MatchesExample { public static void main(String[] args) { String input = "12345"; String pattern = "\\d+"; // one or more digits boolean result = input.matches(pattern); System.out.println("Does the entire string match digits? " + result); } }
find() Equivalent
Here is the equivalent example using find() to check if the string contains any digits:
import java.util.regex.*; public class FindExample { public static void main(String[] args) { String input = "abc123def"; Pattern pattern = Pattern.compile("\\d+"); // one or more digits Matcher matcher = pattern.matcher(input); boolean found = matcher.find(); System.out.println("Does the string contain digits? " + found); } }
When to Use Which
Choose matches() when you need to verify that the entire input string fits a pattern exactly, such as validating user input formats. Choose find() when you want to search for occurrences of a pattern anywhere inside the string, like extracting data or searching text.
Use matches() for strict full-string checks and find() for flexible partial matches or multiple occurrences.
Key Takeaways
matches() checks if the whole string matches the pattern exactly.find() searches for any substring matching the pattern inside the string.matches() for validation and find() for searching or extracting.find() can find multiple matches by repeated calls; matches() cannot.matches() implicitly anchors the pattern; find() does not.