# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"^.*\(((?:RR|RI|PC|PR|PA)R\d{3}){0,1}\) \[([1-2]\d{7}@\d{6})\]\.(( <<.*>>)|( (?!<<).+)){0,1}$"
test_str = ("None (PRR001) [11111111@111111]. <<freetext>>\n"
"None (RIR001) [11111111@111111]. <<freetext>>\n"
"None (RRR001) [11111111@111111]. <<freetext>>\n"
"Prescribing Error (PCR001) [11111111@111111]. <<freetext>>\n"
"None (PAR001) [11111111@111111]. <<freetext>>\n\n"
"None (RIR001) [11111111@111111]. a\n"
"None (RIR001) [11111111@111111]. <a>>\n\n"
"--GOOD Leading and trailing spaces are ignored\n"
"None (PRR001) [11111111@111111]. << freetext >>\n\n"
"--GOOD One element missing\n"
"(PRR001) [11111111@111111]. <<freetext>>\n"
"(PRR001) [11111111@111111]. <<freetext>>\n"
"None () [11111111@111111]. <<freetext>>\n"
"None (PRR001) [11111111@111111]. <<>>\n"
"None (PRR001) [11111111@111111].\n\n"
"--GOOD Freetext embedded <<>>. Leading and trailing spaces\n"
"None (PRR001) [11111111@111111]. <<C<<B>>A>>\n\n"
"--GOOD Old Reason format with correct event type.\n"
"Prescription Reprint <<>>\n"
"Prescription Reprint <<chewed up>>\n\n"
"--GOOD Old Reason format with with freetext embedded <<>>\n"
"Prescription Reprint <<C<<B>>A>>\n\n"
"--GOOD Longer than 400\n"
"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.>>\n\n"
"--GOOD <400 freetext > 350\n"
"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.>>\n\n"
"--GOOD <CR><LF><TAB>\n"
"None (PRR001) [11111111@111111]. <<EA\\r\\nB\\t\\tC>>\n\n"
"--BAD Prove only certain codes looked at\n"
"None (PRR001) [11111111@111111]. <<freetext>>\n\n"
"--BAD One element missing\n"
"None (PRR001) []. <<freetext>>\n\n"
"--BAD Code not RRRnnn,RIRnnn,PCRnnn,PRRnnn or PARnnn\n"
"None (PIR001) [11111111@111111].\n"
"None (RCR001) [11111111@111111].\n"
"None (RAR001) [11111111@111111].\n"
"None (RIR00A) [11111111@111111].\n"
"None (RIRA01) [11111111@111111].\n"
"None (RIR0A1) [11111111@111111].\n"
"None (RIR 01) [11111111@111111].\n"
"None (RIR0 1) [11111111@111111].\n"
"None (RIR00 ) [11111111@111111].\n"
"None (RIR001 ) [11111111@111111].\n"
"None (RIR0001) [11111111@111111].\n"
"None ( RIR001) [11111111@111111].\n"
"None (RIR001 [11111111@111111].\n"
"None RIR001) [11111111@111111].\n"
"None (RIR01) [11111111@111111].\n"
"None (EVE001) [11111111@111111].\n\n"
"--BAD Date time not \\[([1-2]\\d{7}@\\d{6})\\]\n"
"None (RIR001) 11111111@111111].\n"
"None (RIR001) [11111111@111111.\n"
"None (RIR001) [1111111@111111].\n"
"None (RIR001) [111111111@111111].\n"
"None (RIR001) [11111111@11111].\n"
"None (RIR001) [11111111@1111111].\n"
"None (RIR001) [91111111@111111].\n"
"None (RIR001) [11111111@11A111].\n"
"None (RIR001) [111A1111@111111].\n"
"None (RIR001) [11111111x111111].\n"
"None (RIR001) [11111111111111].\n"
"None (RIR001) [].\n"
"None (RIR001).\n\n"
"--BAD Reason text, code, date time with no .\n"
"None (RIR001) [11111111@111111]\n\n"
"--BAD Invalid freetext ( <<.*>>){0,1}\n"
"None (RIR001) [11111111@111111].a\n"
"None (RIR001) [11111111@111111]. \n"
"None (RIR001) [11111111@111111]. <<a>\n"
"None (RIR001) [11111111@111111]. <<>> a\n"
"None (PRR001) [11111111@111111]. <<C<<B>>A\n"
"None (PRR001) [11111111@111111]. <<venalink\n\n"
"--BAD Old Reason format with wrong event type.\n"
"Prescription Reprint\n"
"Prescription Reprint\n"
"Prescription Reprint\n"
"Prescription Reprint\n"
"Prescription Reprint\n"
"Prescription Reprint <<>>\n"
"Prescription Reprint <<>>\n"
"Prescription Reprint <<>>\n"
"Prescription Reprint <<>>\n"
"Prescription Reprint <<chewed up>>\n"
"Prescription Reprint <<chewed up>>\n"
"Prescription Reprint <<chewed up>>\n"
"Prescription Reprint <<chewed up>>\n"
"Prescription Reprint <<C<<B>>A\n"
"Prescription Reprint <<C<<B>>A\n"
"Prescription Reprint <<C<<B>>A\n"
"Prescription Reprint <<C<<B>>A\n\n"
"--BAD Old Reason format with with freetext embedded <<>> and no Trailing >>\n"
"Prescription Reprint <<C<<B>>A\n\n"
"--OTHER BAD EXAMPLES\n\n"
"Existing letter file 'P:\\WORDPROC\\CLINCORR\\0046O000.CLI' deleted when adding letter\n\n"
"Vision 2.00 Install successful on Computername: RECEPTION for filesystem: Live System - Live\n\n"
"Typed in name: MD\n\n"
"Mode=LEGACY;Token Id=AQIC5wM2LY4SfcyXAQB0EBD9MWbdEKQjnCGKw8iNj6Gu3FU=;RoleProfile=339601626031;Role=R1740\n\n"
"Prescription Reprint <<chewed up>>\n\n"
"Mode=CACHED PROFILE;Token Id=;RoleProfile=;Role=,User=SYS\n\n"
"14.9\n\n"
"Vision 22.00.0004 Install successful on Computername: CON06 for filesystem: Live System - Live\n\n"
"ÃÂ?Â?Â?Â?Â?Â<ÿUÂ<ìƒì(SVWÂ?zøÂ?Â\"[K\n\n"
"Existing letter file 'P:\\WORDPROC\\CLINCORR\\00311000.CLI' deleted when adding letter\n\n"
"Quarter 1 2000\n")
matches = re.finditer(regex, test_str, re.MULTILINE)
for matchNum, match in enumerate(matches, start=1):
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
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 Python, please visit: https://docs.python.org/3/library/re.html