The difference between matches (), LookAt (), and find () in the Matcher class in Java

Source: Internet
Author: User

Reference Post address: http://www.oseye.net/user/kevin/blog/170

1, Matcher (): Returns true only if the entire string exactly matches, otherwise false is returned. But if a partial match succeeds, the matching position is moved to the next matching position

2, Lookingat (): Always start from the first character match, no matter whether the match is successful or not, will not continue to match down

3, find (): Partial match, if the match succeeds, returns true, the matching position is moved to the next matching position.

Package Com.qunar.fresh.junweiyu.test;import Java.util.regex.matcher;import Java.util.regex.pattern;public class        test_regx {public static void main (string[] args) {Pattern pattern = Pattern.compile ("\\d{3,5}");        String s = "123-34345-234-00";        Matcher Matcher = Pattern.matcher (s); /*matches: The entire match only returns true if the entire character sequence exactly matches successfully, otherwise false is returned.  But if the first part matches successfully, the next matching position will be moved */SYSTEM.OUT.PRINTLN (matcher.matches ());        /*false*//* Test Match location */Matcher.find ();    System.out.println (Matcher.start ());        /*4*//* Resets the matching position */matcher.reset (); /*find: Partial match, starting at the current position to match, finding a matching substring, moving the next matching position */System.out.println (Matcher.find ());/*true*/System.out.println ( Matcher.group () + "---" + matcher.start ()),/*123---0*/System.out.println (matcher.find ());/*true*/System.ou T.println (Matcher.group () + "---" + matcher.start ())/*34345---4*//*lookingat: Partial match, always match from first character, match succeeded no longer match, match failed, Nor does it continue to match. */System.out.println (Matcher.lookIngat ());/*true*/System.out.println (Matcher.group () + "---" + matcher.start ());/*123---0*/System.out.print ln (Matcher.lookingat ());/*true*/System.out.println (Matcher.group () + "---" + matcher.start ());/*123-0*/}}


The difference between matches (), LookAt (), and find () in the Matcher class in Java

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.