Part of the code I\'m working on uses a bunch of regular expressions to search for some simple string patterns (e.g., patterns like \"foo[0-9]{3,4} bar\"). Currently, we use sta
If you expect less than 50% of lines matching your regex, you can first try to test for some subsequence via String.indexOf()
which is about 3 to 20 times faster for simple sequence compared to regex matcher:
if (line.indexOf("foo")>-1) && pattern.matcher(line).matches()) {
...
If you add to your code such heuristics, remember to always well document them, and verify using profiler that code is indeed faster compared to simple code.