First question is: is the regex really necessary? I couldn't face a case where it was necessary.
Maybe be you could make it an option? I use your lib for realtime computation and so every little improvement is a big improvement ;)
Now if you decided to go to a simple replace like i did then you would face the problem that String.replace always uses regex and is really slow.
public static String fastReplace(String source, String os, String ns) {
if (source == null) {
return null;
}
int i = 0;
if ((i = source.indexOf(os, i)) >= 0) {
char[] sourceArray = source.toCharArray();
char[] nsArray = ns.toCharArray();
int oLength = os.length();
StringBuilder buf = new StringBuilder(sourceArray.length);
buf.append(sourceArray, 0, i).append(nsArray);
i += oLength;
int j = i;
// Replace all remaining instances of oldString with newString.
while ((i = source.indexOf(os, i)) > 0) {
buf.append(sourceArray, j, i - j).append(nsArray);
i += oLength;
j = i;
}
buf.append(sourceArray, j, sourceArray.length - j);
source = buf.toString();
buf.setLength(0);
}
return source;
}