Matcher.appendreplacement (StringBuffer SB, String replacement) parsing

Source: Internet
Author: User

Pattern p = pattern.compile ("cat");
Matcher m = P.matcher ("One cat, both cats in the Yard");
StringBuffer sb = new StringBuffer ();
while (M.find ()) {
M.appendreplacement (SB, "dog");
}
M.appendtail (SB);
System.out.println (Sb.tostring ());

Functions of the Appendreplacement function
The next location from the last successful match will have the last position of the successful start of the match + this time the matched string
The last position of the first match success is-1
Text: The original text assigned to the match is the text to be matched
Lastappendposition: Last appended position, 0 for the first time
First: The starting position for this successful match
Append the original text from the last successful position of the previous match to the location where the match was successfully started to SB
For example Text:one cat cats in the yard lastappendposition:0 First:4 res:one Space
Sb.append (text, lastappendposition, first);
Append the string on the match to the SB after processing
Sb.append (result);
The last position to append is the final position of the match.
Lastappendposition = Last;

Matcher.appendreplacement (StringBuffer SB, String replacement) parsing

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.