php – Improve performance of this regex
I came up with this Regex but I think I can make its performance better because my server is struggling executing it thousands of times per minute on long strings.
$que = preg_replace("/\\b(xxg|xxl|xxs)\\b|\\b(([^0-9])\\3+)\\b|([^0-9])\\4+/","$1$2$4",$que);
Basically this regex converts strings like:
aaa ccdd -> aaa cd
uuii -> ui
xxg aabbb -> xxg ab (xxg is a special case as I putted it inside my regex).
Basically, the regex replaces all letters that repeats to one single letter except when the word consists of the letter repeating itself and except on the substrings xxg and xxl and xxs.
Any alternative I can try to check if I get a better performance?
One improvement that I realized is the code below (wich saves about 20% on CPU time):
$que = preg_replace("/(?|\\b(xxg|xxl|xxs)\\b|\\b(([^0-9])\\2+)\\b|([^0-9])\\1+)/","$1",$string);
Read more here: Source link
