In the previous section, we learned the first step of using the Pattern class and the Matcher class, establishing a pattern object . In this section, we continue to learn the second step, getting the matching object .
Get an instance matcher of the Matcher class that can retrieve string input, called the matching object:
Matchermatcher=pattern.matcher(input);
The pattern object pattern calls the matcher(CharSequence input) method to return a Matcher object matcher, which is called the matching object . The parameter input is used to give the string to be retrieved by the matcher. The parameter input can be an object created by any class that implements the CharSequence interface. The String class and StringBuffer class learned earlier implement the CharSequence interface.
The matching object matcher can call various methods to retrieve the string input . For example: the matcher calls the boolean find() method in sequence to retrieve the substring in the input that matches the regex. For example: the first call to the find() method will retrieve the first substring good in the input, that is, matcher.find() retrieves the first good and returns true. At this time, the value returned by matcher.start() is 6 , the position where the first good starts, the value returned by matcher.end() is 10, the position where the first good ends, matcher.group() returns good, that is, returns the retrieved string.
The Matcher object matcher can use the following methods to find whether there is a subsequence in the string input that matches the pattern regex. regex is the regular expression used when creating the pattern object pattern.
Find the next subsequence that matches input and regex. If successful, the method returns true, otherwise it returns false. When the matcher calls this method for the first time, it looks for the first subsequence in the input that matches the regex. If find() returns true, when the matcher calls the find() method again, it will start searching from the last subsequence that successfully matched the pattern. The next substring matching the pattern. In addition, when the find method returns true, the matcher can call the start() method and the end method to get the start and end positions of the matching pattern subsequence in the input. When the find method returns true, the matcher calls group() to return the substring of the matching pattern found by the find method this time.
The matcher calls this method to determine whether the input completely matches the regex.
The matcher calls this method to determine whether there is a subsequence matching the regex from the beginning of the input. If the lookingAt() method returns true, the matcher calls the start() method and the end method to get the start position and end position in the input of the subsequence of the matching pattern found by the lookingAt() method. If the lookingAt() method returns true, the matcher calls group() to return the subsequence of the matching pattern found by the lookingAt() method.
The matcher calls this method to determine whether the input has a subsequence matching the regex starting from the position specified by the parameter start. When the parameter start is 0, this method has the same function as lookingAt().
The matcher can return a string by calling this method. The string is obtained by replacing all the substrings in the input that match the pattern regex with the string specified by the parameter replacement. It should be noted that the input itself does not change.
The matcher calls this method to return a string, which is obtained by replacing the first substring in the input that matches the pattern regex with the string specified by the parameter replacement. It should be noted that the input itself does not change. .