JAVA regular expression, the difference between matcher.find and matcher.matches

1. The find () method is partial matching, which is to find substrings in the input string that match the pattern . If the matched string has groups, you can also use the group () function.
matches () is to match all, is to match the entire input string with the pattern , if you want to verify whether an input data is a numeric type or other types, generally matches ().

2.Pattern pattern = Pattern.compile (". * ?, (. *)");
Matcher matcher = pattern.matcher (result);
if (matcher.find ()) {
return matcher.group (1);
}

3 .Details:
matches
public static boolean matches (String regex, CharSequence input)
compile the given regular expression and try to match the given input with it.
Call this convenient method in the form of
Pattern.matches (regex, input);
Pattern.compile (regex) .matcher (input) .matches ();
if you want to use a pattern multiple times, reuse this pattern after compiling once more than It is more efficient to call this method.
Parameters:
regex-the expression to be compiled
input-the sequence of characters to match
Throw:
PatternSyntaxException-If the syntax of the expression is invalid,

find
public boolean find () attempts to find the next subsequence of the input sequence that matches the pattern .
This method starts from the beginning of the matcher area. If the previous call of the method succeeds and the matcher has not been reset since then, the first character of the previous match operation that did not match is started.
If the match is successful, you can get more information through the start, end, and group methods.
matcher.start () returns the index position of the matched substring in the string.
matcher.end () returns the index position of the last character of the matched substring in the string.
matcher.group () returns Matching substring
Returns: true
if and only if the subsequence of the input sequence matches the pattern of this matcher.

Published 15 original articles · praised 7 · 10,000+ views

Guess you like

Origin blog.csdn.net/qq_40938267/article/details/95163476