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