The Problem: I have a long string of serial numbers, that looks like this:
"1_2_3_4_5 0_100_1_0_4 1_2_3_4_5 0_100_1_0_4 0_100_1_0_4
3_1_34_5_6";
I want to remove all serial numbers that do not occur EXACTLY 3 or 1 times in the string.
The Result: In this case the return string should be:
0_100_1_0_4 0_100_1_0_4 0_100_1_0_4 3_1_34_5_6
The question: How can I use pure regex in php to remove all serial numbers that do not occur 3 or 1 times. I do not want to use array unique or array_count_values as they are too slow when dealing with large amounts. Pure regex is faster for my case.
EDITED to give full explanation of what code does, in case someone knows of a better way to approach problem: Code reads in 10 files with millions of serial numbers within them, merges them and prints out all numbers that appear 3 times. This is performed thousands of times and array_count_values statement is killing the speed. Everything else is fast, I know because I timed profiled the code.
$result_hold="";
for($i=0; $i<10000; $i++)
{
$linespreset=file("sometext2.txt",FILE_IGNORE_NEW_LINES);
$holdpreset=explode(" ",$linespreset[0]);
$holdpreset=array_map("trim", $holdpreset);
$print1=file_get_contents('six.txt');
$print2=file_get_contents('seven.txt');
$print3=file_get_contents('thetext.txt');
$print4=file_get_contents('sometext.txt');
$print5=file_get_contents('moretext.txt');
$print6=file_get_contents('alltext.txt');
$print7=file_get_contents('trialtext.txt');
$print8=file_get_contents('two.txt');
$print9=file_get_contents('four.txt');
$print10=file_get_contents('five.txt');
$resultround=$print1."\r\n".$print2."\r\n".$print3."\r\n".$print4."\r\n".$print5."\r\n".$print6."\r\n".$print7."\r\n".$print8."\r\n".$print9."\r\n".$print10;
$frequency = array_count_values($resultround=explode("\r\n",$resultround));
$result = array_filter($frequency, function ($x) { return $x ==3; });
$result=array_keys($result);
$resultnew=implode("\r\n",$result);
$result_hold .=$resultnew."\r\n";
unset($result);
unset($frequency);
unset($resultnew);
}