Implements a non-terminal append-and-replace step.
This method performs the following actions:1. It reads characters from the input sequence, starting at theappend position, and appends them to the given string buffer. Itstops after reading the last character preceding the previous match,that is, the character at index start() - 1.2. It appends the given replacement string to the string buffer.3. It sets the append position of thismatcher to the index ofthe last character matched, plus one, that is, to end().
The replacement string may contain references to subsequencescaptured during the previous match: Each occurrence of ${name} or $gwill be replaced by the result of evaluating the corresponding group(name) or group(g)respectively. For $g,the first number after the $ is always treated as part ofthe group reference. Subsequent numbers are incorporated into g ifthey would form a legal group reference. Only the numerals‘0‘through ‘9‘ are considered as potential components of the groupreference. If the second group matched the string "foo", forexample, then passing the replacement string "$2bar" wouldcause "foobar"to be appended to the string buffer. A dollarsign ($) may be included as a literal in the replacementstring by preceding it with a backslash (\$).
Note that backslashes (\) and dollar signs ($) inthe replacement string may cause the results to be different thanifitwere being treated as a literal replacement string. Dollar signs may betreated as references to captured subsequences as described above, andbackslashes are used to escape literal characters in the replacementstring.
This method is intended to be used in a loop together with the appendTail and find methods. Thefollowing code,for example, writes one dog two dogs in theyard to the standard-output stream:
Pattern p= Pattern.compile("cat");
Matcher m= p.matcher("one cat two cats in the yard");
StringBuffer sb= newStringBuffer();while(m.find()) {
m.appendReplacement(sb,"dog");
}
m.appendTail(sb);
System.out.println(sb.toString());