Differences between matches (), LookAt (), and find () in Matcher classes in Java

Source: Internet
Author: User

Kauboven Address: http://www.oseye.net/user/kevin/blog/170

1, Matcher (): Returns true only if the entire string is completely matched, otherwise false.

But suppose a partial match succeeds. The matching position is moved to the next matching position

2, Lookingat (): Always start with the first character to match. No match succeeds, no further matching down

3. Find (): Partial match, assuming the match is successful. Returns true, where 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 the entire sequence of characters succeeds, returns True, otherwise false.

But assuming the previous 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: partially matched. Matches are always made from the first character, and the match succeeds and no longer continues. The match failed, and the match was not continued.

*/System.out.println (Matcher.lookingat ());/*true*/System.out.println (Matcher.group () + "---" + matcher.sta RT ());/*123---0*/System.out.println (Matcher.lookingat ());/*true*/System.out.println (Matcher.group () + "--- "+ Matcher.start ());/*123-0*/}}



Java is the difference between matches (), LookAt (), and find () in Matcher classes

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.