Differences between matches, lookAt, and find of Matcher classes in Java

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.