$re = '/^(?P<ID>[A-Z][A-Z0-9]{3})?(?P<REF1>(?:(?!//).)+)(//(?P<REF2>.+))?(\n?(?P<EXTRA>.+))?$/m';
$str = 'TEX1CNS0P5-AA//CAT-523-VID-00EOS-0
XUX PETER LAB RANDOM TEXT DM5.
TEX2BFTBSH9999SBRT2L
RATRACE201
TEX3GWS0P2-AA//D-14839048-99-3
THERE WAS 200 COALS IN HIS STOCKING.
TEX4POF OF 20/03/09//CAT342134832489
P/O:1600 PARK AVENUE';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);
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