I need to replace all & in a String that isnt part of a HTML entity. So that the String "This & entites >
& <
" will return "This &
entites > & <
"
And I've come up with this regex-pattern: "&[a-zA-Z0-9]{2,7};" which works fine. But I'm not very skilled in regex, and when I test the speed over 100k iterations, it uses double amount of time over a previous used method, that didnt use regex. (But werent working 100% either).
Testcode:
long time = System.currentTimeMillis();
String reg = "&(?!&#?[a-zA-Z0-9]{2,7};)";
String s="a regex test 1 & 2 1&2 and &_gt; - &_lt;"
for (int i = 0; i < 100000; i++) {test=s.replaceAll(reg, "&");}
System.out.println("Finished in:" + (System.currentTimeMillis() - time) + " milliseconds");
So the question would be whether there is some obvious ways of optimize this regex expression for it to be more effective?