$re = '/^.*\(((?:RR|RI|PC|PR|PA)R\d{3}){0,1}\) \[([1-2]\d{7}@\d{6})\]\.(( <<.*>>)|( (?!<<).+)){0,1}$/m';
$str = 'None (PRR001) [11111111@111111]. <<freetext>>
None (RIR001) [11111111@111111]. <<freetext>>
None (RRR001) [11111111@111111]. <<freetext>>
Prescribing Error (PCR001) [11111111@111111]. <<freetext>>
None (PAR001) [11111111@111111]. <<freetext>>
None (RIR001) [11111111@111111]. a
None (RIR001) [11111111@111111]. <a>>
--GOOD Leading and trailing spaces are ignored
None (PRR001) [11111111@111111]. << freetext >>
--GOOD One element missing
(PRR001) [11111111@111111]. <<freetext>>
(PRR001) [11111111@111111]. <<freetext>>
None () [11111111@111111]. <<freetext>>
None (PRR001) [11111111@111111]. <<>>
None (PRR001) [11111111@111111].
--GOOD Freetext embedded <<>>. Leading and trailing spaces
None (PRR001) [11111111@111111]. <<C<<B>>A>>
--GOOD Old Reason format with correct event type.
Prescription Reprint <<>>
Prescription Reprint <<chewed up>>
--GOOD Old Reason format with with freetext embedded <<>>
Prescription Reprint <<C<<B>>A>>
--GOOD Longer than 400
None (PRR001) [11111111@111111]. <<Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam ultrices orci non leo volutpat, at sagittis dolor cursus. Duis ullamcorper laoreet scelerisque. Donec eu nulla nec erat eleifend consectetur. Donec tincidunt, risus id imperdiet consectetur, eros mauris pharetra purus, in rhoncus diam massa ac elit. Suspendisse eleifend erat vitae cras amet.>>
--GOOD <400 freetext > 350
None (PRR001) [11111111@111111]. <<Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed condimentum, erat nec rhoncus porta, urna sem laoreet elit, sed tempus augue erat nec odio. Morbi facilisis porttitor dolor sodales blandit. Phasellus lobortis ligula sit amet elit sagittis, eu ultrices mi aliquet. Curabitur a odio mauris. Nam quis ullamcorper elit. In dignissim ac nullam.>>
--GOOD <CR><LF><TAB>
None (PRR001) [11111111@111111]. <<EA\\r\\nB\\t\\tC>>
--BAD Prove only certain codes looked at
None (PRR001) [11111111@111111]. <<freetext>>
--BAD One element missing
None (PRR001) []. <<freetext>>
--BAD Code not RRRnnn,RIRnnn,PCRnnn,PRRnnn or PARnnn
None (PIR001) [11111111@111111].
None (RCR001) [11111111@111111].
None (RAR001) [11111111@111111].
None (RIR00A) [11111111@111111].
None (RIRA01) [11111111@111111].
None (RIR0A1) [11111111@111111].
None (RIR 01) [11111111@111111].
None (RIR0 1) [11111111@111111].
None (RIR00 ) [11111111@111111].
None (RIR001 ) [11111111@111111].
None (RIR0001) [11111111@111111].
None ( RIR001) [11111111@111111].
None (RIR001 [11111111@111111].
None RIR001) [11111111@111111].
None (RIR01) [11111111@111111].
None (EVE001) [11111111@111111].
--BAD Date time not \\[([1-2]\\d{7}@\\d{6})\\]
None (RIR001) 11111111@111111].
None (RIR001) [11111111@111111.
None (RIR001) [1111111@111111].
None (RIR001) [111111111@111111].
None (RIR001) [11111111@11111].
None (RIR001) [11111111@1111111].
None (RIR001) [91111111@111111].
None (RIR001) [11111111@11A111].
None (RIR001) [111A1111@111111].
None (RIR001) [11111111x111111].
None (RIR001) [11111111111111].
None (RIR001) [].
None (RIR001).
--BAD Reason text, code, date time with no .
None (RIR001) [11111111@111111]
--BAD Invalid freetext ( <<.*>>){0,1}
None (RIR001) [11111111@111111].a
None (RIR001) [11111111@111111].
None (RIR001) [11111111@111111]. <<a>
None (RIR001) [11111111@111111]. <<>> a
None (PRR001) [11111111@111111]. <<C<<B>>A
None (PRR001) [11111111@111111]. <<venalink
--BAD Old Reason format with wrong event type.
Prescription Reprint
Prescription Reprint
Prescription Reprint
Prescription Reprint
Prescription Reprint
Prescription Reprint <<>>
Prescription Reprint <<>>
Prescription Reprint <<>>
Prescription Reprint <<>>
Prescription Reprint <<chewed up>>
Prescription Reprint <<chewed up>>
Prescription Reprint <<chewed up>>
Prescription Reprint <<chewed up>>
Prescription Reprint <<C<<B>>A
Prescription Reprint <<C<<B>>A
Prescription Reprint <<C<<B>>A
Prescription Reprint <<C<<B>>A
--BAD Old Reason format with with freetext embedded <<>> and no Trailing >>
Prescription Reprint <<C<<B>>A
--OTHER BAD EXAMPLES
Existing letter file \'P:\\WORDPROC\\CLINCORR\\0046O000.CLI\' deleted when adding letter
Vision 2.00 Install successful on Computername: RECEPTION for filesystem: Live System - Live
Typed in name: MD
Mode=LEGACY;Token Id=AQIC5wM2LY4SfcyXAQB0EBD9MWbdEKQjnCGKw8iNj6Gu3FU=;RoleProfile=339601626031;Role=R1740
Prescription Reprint <<chewed up>>
Mode=CACHED PROFILE;Token Id=;RoleProfile=;Role=,User=SYS
14.9
Vision 22.00.0004 Install successful on Computername: CON06 for filesystem: Live System - Live
ÃÂ?Â?Â?Â?Â?Â<ÿUÂ<ìƒì(SVWÂ?zøÂ?Â"[K
Existing letter file \'P:\\WORDPROC\\CLINCORR\\00311000.CLI\' deleted when adding letter
Quarter 1 2000
';
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