I would like to replace some texts in StringBuilder. How to do this?
In this code I got java.lang.StringIndexOutOfBoundsException at line with mat
You shouldn't do it this way. The input to Matcher may be any CharSequence, but the sequence should not change. Matching like you do is like iterating over a Collection while removing elements at the same time, this can't work.
However, maybe there's a solution:
while (matcher.find()) {
sb.replace(matcher.start(), matcher.end(), "x");
matcher.region(matcher.start() + "x".length(), sb.length());
}