$re = '/(?:^(?:[^|\r\n]*\|){10}[^|\r\n]*\K\||(?!^)\G[^|\r\n]*\K\|)(?=.*(?:\|[^|\r\n]*){2}$)/m';
$str = 'A1|A2|A3|A4|A5|A6|A7|A8|A9|A10|A11|A12|A13|A14|A15|A16|AA
B1|B2|B3|B4|B5|B6|B7|B8|B9|B10|B11|B12|B13|B14|B15|AA
C1|C2|C3|C4|C5|C6|C7|C8|C9|C10|C11|C12|C13|C14|AA
D1|D2|D3|D4|D5|D6|D7|D8|D9|D10|D11|D12|D13|AA
E1|E2|E3|E4|E5|E6|E7|E8|E9|E10|E11|E12|AA
';
$subst = " ";
$result = preg_replace($re, $subst, $str);
echo "The result of the substitution is ".$result;
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for PHP, please visit: http://php.net/manual/en/ref.pcre.php