Are there known difference(s) between String.replaceAll() and Matcher.replaceAll() (On a Matcher Object created from a Regex.Pattern) in terms of performance?
Also, what are the high-level API ‘ish differences between the both? (Immutability, Handling NULLs, Handling empty strings, etc.)
According to the documentation for
String.replaceAll, it has the following to say about calling the method:Therefore, it can be expected the performance between invoking the
String.replaceAll, and explicitly creating aMatcherandPatternshould be the same.Edit
As has been pointed out in the comments, the performance difference being non-existent would be true for a single call to
replaceAllfromStringorMatcher, however, if one needs to perform multiple calls toreplaceAll, one would expect it to be beneficial to hold onto a compiledPattern, so the relatively expensive regular expression pattern compilation does not have to be performed every time.