I have a string like this
This:string:must~:be:split:when:previous:char:is:not~:this
I need to split the line with the delimiter “:” but only if the character before the delimiter is NOT “~”
I have the following regex now:
String[] split = str.split(":(?<!~:)");
It works, but since I arrived at it purely by trial and error, I’m not convinced that its the most efficient way of doing it. Also, this function will be repeatedly called on large strings frequently, so performance does come into consideration. What is a more efficient way of doing it?
Update: To make this more fair I wanted to use a compiled Pattern and see the results of that. So I updated the code to use compiled pattern, non-compiled pattern and my custom method.
While this isn’t using regex it proves to be faster then the regex given.
Output