So I ran into a bug caused by expecting the matches() method to find exactly the same match as using find(). Normally this is the case, but it appears that if a non-greedy p
This makes perfect sense.
The matches(...)
method must attempt to consume the whole string, so it does, even with a non-greedy pattern.
The find(...)
method may find a substring, so it stops at the point if finds any matching substring.
They are supposed to be different. Matcher#matches
attempts to match the complete input string using the implicit anchors ^
and $
around your regex, whereas Matcher#find
matches whatever your regex can match.
As per Javadoc:
public boolean matches()
Attempts to match the entire region against the pattern. If the match succeeds then more information can be obtained via the start, end, and group methods.
and
public boolean find()
Attempts to find the next subsequence of the input sequence that matches the pattern.