Reputation: 10915
Which of the following would be more efficient and better to use?
value.replaceAll("['‘’`]","")
value.replaceAll("['‘’`]+","")
My guess would be that for strings that don't have the replaced chars, or at least don't have sequences of them the two are the same or the first is better being less complex.
but what if I am looking at strings that do have sub-sequences of the chars being replaced ? will the second one be better ?
'abababababababab'.replaceAll("ab","")
v.s.
'abababababababab'.replaceAll("(ab)+","")
I am using Java if this matters for the sake of this Q.
Upvotes: 4
Views: 276
Reputation: 469
Per analysis I would say the first option is faster than the second. Although I must say that this difference is not easily measurable unless you have a huge string as input (or complex regex).
So lets call this regex1:
'abababababababab'.replaceAll("ab","")
And this regex2:
'abababababababab'.replaceAll("(ab)+","")
We know from Java API that the replaceAll will see both the conditions as a regex and the try to replace the string following the regex engine.
We can see that regex1 have the char sequence only; while and the regex2 have a group, a char sequence and the a quantifier metacharacters that must be interpreted accordingly (more info here). Therefore regex2 need more processing than regex1.
In general both options are really fast for most uses. You can have a more detailed view on the process by reading this article: Regular Expression Matching Can Be Simple And Fast
Still, using the Pattern and Matcher for more complex regex is a faster option... (more info here).
Also an additional reading I recommend in this scenario is: Optimizing Regular Expressions in Java
Upvotes: 1