Consider 3 regex expressions designed to remove non Latin characters from the 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 vastly difference performance metrics.
Why is that?