Consider 3 regular expression expressions designed to remove non-Latin characters from a string.
String x = "someβ Β₯Β₯ΒΆΒΆΛΛΛword"; long now = System.nanoTime(); System.out.println(x.replaceAll("[^a-zA-Z]", "")); // 5ms System.out.println(System.nanoTime() - now); now = System.nanoTime(); System.out.println(x.replaceAll("[^a-zA-Z]+", "")); // 2ms System.out.println(System.nanoTime() - now); now = System.nanoTime(); System.out.println(x.replaceAll("[^a-zA-Z]*", "")); // <1ms System.out.println(System.nanoTime() - now);
All 3 produce the same result with significantly different performance metrics.
Why is this?
source share