Matches: the entire match. True is returned only when the entire character sequence is completely matched. Otherwise, False is returned. However, if the first part matches successfully, the next matched position will be moved.
LookingAt: partial match, always matching from the first character. If the match is successful, it will not continue. If the match fails, it will not continue.
Find: partially matches, starts matching from the current position, finds a matched substring, and moves the next matched position.
Reset: add a new target to the current Matcher object. The target is the parameter of the method. If no parameter is set, the reset sets the Matcher to the beginning of the current string.
Use the sample code to show their differences more clearly:
The code is as follows: |
Copy code |
Package net. oseye; Import java. util. regex. Matcher; Import java. util. regex. Pattern; Public class IOTest { Public static void main (String [] args ){ Pattern pattern = Pattern. compile ("d {3, 5 }"); String charSequence = "123-34345-234-00 "; Matcher matcher = pattern. matcher (charSequence ); // Although the match fails, because the "123" in charSequence matches pattern, the next match starts from position 4. Print (matcher. matches ()); // Test the matching position Matcher. find (); Print (matcher. start ()); // Use the reset method to reset the matching position Matcher. reset (); // The first find match, the matched target, and the starting position of the match Print (matcher. find ()); Print (matcher. group () + "-" + matcher. start ()); // The second find match, the matched target, and the starting position of the match Print (matcher. find ()); Print (matcher. group () + "-" + matcher. start ()); // The first lookingAt match, the matched target, and the starting position of the match Print (matcher. lookingAt ()); Print (matcher. group () + "-" + matcher. start ()); // The second lookingAt match, the matched target, and the starting position of the match Print (matcher. lookingAt ()); Print (matcher. group () + "-" + matcher. start ()); } Public static void print (Object o ){ System. out. println (o ); } }
|
Output result:
The code is as follows: |
Copy code |
False 4 True 123-0 True 34345-4 True 123-0 True 123-0 |