0
0
JavaComparisonBeginner · 3 min read

Matches vs Find in Java Regex: Key Differences and Usage

In Java regex, 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.

Aspectmatches()find()
PurposeChecks if entire string matches patternSearches for any substring matching pattern
Return Typebooleanboolean
Match ScopeWhole input stringAny part of input string
Typical Use CaseValidation of full inputFinding occurrences inside input
Multiple MatchesNo, single checkYes, can find multiple matches with repeated calls
Reset BehaviorNo need to resetCan 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:

java
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);
    }
}
Output
Does the entire string match digits? true
↔️

find() Equivalent

Here is the equivalent example using find() to check if the string contains any digits:

java
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);
    }
}
Output
Does the string contain digits? true
🎯

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.
Use 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.