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