I'd like to use something like StringBuilder to hold a string, and then perform a large number of regex replaceAll operations on it, in an efficient way. I'd like to leverage StringBuilder's variable sized array and prevent temporary string allocations. That is, I'd like the regex replaceAll operation to mutate the array held by StringBuilder as needed, without allocating temporary strings. How can I do this?
Unfortunately StringBuilder does not have a built-in method to do this. It only has a replace() method without regex, and I can't see a way to do this without effectively replacing the entire StringBuilder buffer with a newly allocated String using Matcher, which I'd like to avoid.
replaceAllon aStringinstead ofStringBuilderone way to be more efficient is to compile thePatternbeforehand and use that to replace things with sincereplaceAllwill callPattern.compileeverytime.