Java Regular Expressions Appendreplacement and Appendtail methods

Source: Internet
Author: User

Appendreplacement is a method of replacing the corresponding string in Java

Appendreplacement (StringBuffer sb,string replacement)

Replaces the current matching substring with the specified string and adds the replaced substring and the string segment that precedes the last matched substring to a StringBuffer object

Appendtail (StringBuffer SB)

Adds the remaining string to a StringBuffer object after the last matching work

If you don't understand it, let's have a simple demo.

 Public classthereplacements { Public Static voidMain (string[] args)throwsException {//generate a Pattern object and compile a simple regular expression "cat"Pattern p = pattern.compile ("Cat"); //generates a Matcher object using the Matcher () method of the Pattern classMatcher m = P.matcher ("Fatcatfatcatfat"); StringBuffer SB=NewStringBuffer ();  while(M.find ()) {//at this point, SB is replaced with the dog Fatdogfatdog,cat, and the last match to the previous substring is added to the SB objectM.appendreplacement (SB, "dog")); }        //at this point SB is Fatdogfatdogfat, and the last matched substring is added to the SB object .M.appendtail (SB); //output content is FatdogfatdogfatSystem.out.println ("SB:" +SB); }}

The comments are detailed enough to be repeated.

Java Regular Expressions Appendreplacement and Appendtail methods

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.