Optimization methods:
- use
Pattern.compile()for each replace. Create a class, make patterns fields, and compile the patterns only once. That way you will save a lot of time, since regex compile takes place each time you callreplaceAll()and it is a very costly operation - use non-greedy regexes. Instead of
(\\d+)use(\\d+?). - try to not use regexes if possible (
lb.->pound)? - merging several regexes with the same substitutions into one - applicable to your
sqkmorfeetreplaces - you could try to base your api on
StringBuilder; then use addReplacement to process your text.
Moreover a dot in many of your replaces is unescaped. Dot matches any character. Use \\..
Class idea:
class RegexProcessor {
private Pattern feet1rep = Pattern.compile("\\b(\\d+)[ ]?'[ ]?(\\d+)\"");
// ...
public String process(String org) {
String mod = feet1rep.match(org).replaceAll("$1 feet $2 ");
/...
}
}