The appendReplacement(StringBuffer sb, String replacement) method

This method is intended to be used in a loop together with the appendTail and find methods. Once we get a match using the find() method, we can call the appendReplacement() method to operate on each match and replace the matched text. Finally, it appends the replaced text to StringBuffer. It reads characters from the input sequence, starting at the append position, and appends them to the given string buffer. It stops after reading the last character preceding the previous match, that is, the character at index start() - 1.

The replacement string may contain references to subsequences captured during the previous match. All the rules of replacement reference we String.replaceAll ...

Get Java 9 Regular Expressions now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.