Every day a bit of regular expression accumulation (e) Regular expression

Source: Internet
Author: User
Tags stringbuffer
        /** * matches is used to match the entire character, find the substring, where the search method call, * can call the Matcher tostring method, return the string of * LASTMATC
         h to check which substrings to get the matching word *, but the simplest method is to call group (), pay attention to the effect of reset, Lookingat every time to look at the * Note that the start and end methods must be able to find, only return value, otherwise the error *//This is pattern the typical call order is pattern P = pattern.compile ("\\d{3,5}");//represents three to five digits String s = "123-34345-
        234-00 ";
        Matcher m = P.matcher (s);
        P (m.matches ());//false//reset the matching device.
        M.reset (); P (M.find ());//true try to find the next subsequence or substring of the input sequence that matches the pattern such as 123 call ToString to see which matches P (m.tostring ());//java.util.regex.matcher[ pattern=\d{3,5} region=0,16 lastmatch=123] Note lastmacth = 123 P (M.group ());//123 P (M.start () + "-" + m.end () )//Because end returns the offset after the last match character. So the result is 0-3 instead of 0-2 below the same p (M.find ());//true is 34345 P (m.tostring ());//java.util.regex.matcher[pattern=\d{3,5} reg
        ion=0,16 lastmatch=34345] Note lastmatch=34345 p (M.group ()),//34345 P (M.start () + "-" + m.end ());//4-9 P (M.FIND ());//true 234 match P (m.tostring ());//java.util.regex.matcher[pattern=\d{3,5} region=0,16 lastmatch=234] Note LASTMATC h=234 P (M.group ());//234 P (M.start () + "-" + m.end ());//10-13 P (M.find ());//false 00 does not match P ( M.tostring ());//java.util.regex.matcher[pattern=\d{3,5} region=0,16 lastmatch=] Note lastmatch= p (m.start () + "-" + M.
         End ())//At this point, because there is no matching substring/** * After running the above results can be found, you can use the following to see how many substrings to get a match, very concise * output is * 123
        *34345 *234 */while (M.find ()) {p (M.group ()); //Lookingat () attempts to match the input sequence starting at the beginning of the zone with the pattern.
        Every look is from the start, so the following are all True P (M.lookingat ());
        P (M.lookingat ());
        P (M.lookingat ());
P (M.lookingat ());
        Replacement//enable case-insensitive matching.
        Pattern P1 = pattern.compile ("java", pattern.case_insensitive);
        Matcher m1 = P1.matcher ("Java Java Java Java Ilovejava you hatejava afasdfasdf"); StringBuffer buf = new STRIngbuffer ();
                                 int i = 0;//calculation has several matching values/** * appendreplacement (stringbuffer SB,//SB-Destination string buffer.
            String replacement), after matching the word subcode string with replacement, the result is deposited in BUF * * while (M1.find ()) {i++;
            if (i% 2 = 0) {m1.appendreplacement (buf, "Java");
            else {m1.appendreplacement (buf, "JAVA");
        } m1.appendtail (BUF);//If not add this sentence p (buf) output to Java Java Java Java Ilovejava you hatejava p (i);//6 P (BUF);//Add M1.appendtail (BUF); After output Java Java Java Java Ilovejava you hatejava afasdfasdf
public static void P (Object o) {
        System.out.println (o);
    }


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.