# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"""
\#(?P<cycle>\d+)\s
(?P<date>[\d/]+)\s
(?P<time>[\d:]+)\s+
Total:\s(?P<total>[\d.]+)[-\s]+
RCPE:\s+(?P<rcpe>\d+)\s
ID:\s(?P<id>\d+)\s
WKOD:\s+(?P<wkod>\d+)\s
OPRT:\s+(?P<oprt>\d+)\s
TARE:\s(?P<tare>[.\d]+)
"""
test_str = ("--------- #1157 11/06/2015 09:44:21 Total: 2482.3 ---------\n"
"RCPE: 101 ID: 204 WKOD: 0 OPRT: 0 TARE: 13.6\n"
"MAT ADDI(2) REGR(4) ADDI(5) ADDI(6) NATU(8)\n"
" 2% 25% 0.5% 1.3% 100 \n"
"FINA R 1.89 25.36 0.54 1.31 100.00\n"
"FINA W 33.7 629.4 9.6 23.3 1786.1\n"
"1st DW 22.8 629.4 9.6 23.3 1786.1\n"
"1st DT 79.0 1578.0 3622.0 9753.0 8468.0\n"
"1st FR 449.37 396.19 2.47 2.38 212.82\n"
" DW/DT 288.40 398.88 2.66 2.39 210.93\n"
"FRate 449.37 396.19 2.57 2.38 211.87\n"
"Retry# 02 \n\n"
"--------- #1158 11/06/2015 09:45:40 Total: 2513.7 ---------\n"
"RCPE: 101 ID: 204 WKOD: 0 OPRT: 0 TARE: 12.4\n"
"MAT ADDI(2) REGR(4) ADDI(5) ADDI(6) NATU(8)\n"
" 2% 25% 0.5% 1.3% 100 \n"
"FINA R 1.81 25.48 0.49 1.28 100.00\n"
"FINA W 32.8 640.4 8.8 23.2 1808.4\n"
"1st DW 21.1 640.4 8.8 23.2 1705.8\n"
"1st DT 80.0 1578.0 3524.0 9875.0 8456.0\n"
"1st FR 449.37 396.19 2.57 2.38 211.87\n"
" DW/DT 263.20 405.85 2.51 2.35 201.73\n"
"FRate 449.37 396.19 2.57 2.38 206.80\n"
"Retry# 01 01 \n\n"
"--------- #1159 11/06/2015 09:46:43 Total: 2484.9 ---------\n"
"RCPE: 101 ID: 204 WKOD: 0 OPRT: 0 TARE: 12.3\n"
"MAT ADDI(2) REGR(4) ADDI(5) ADDI(6) NATU(8)\n"
" 2% 25% 0.5% 1.3% 100 \n"
"FINA R 1.83 25.36 0.51 1.26 100.00\n"
"FINA W 32.8 630.2 9.1 22.6 1790.2\n"
"1st DW 24.3 630.2 9.1 22.6 1790.2\n"
"1st DT 80.0 1578.0 3489.0 9775.0 8710.0\n"
"1st FR 449.37 396.19 2.57 2.38 206.80\n"
" DW/DT 303.24 399.39 2.60 2.31 205.53\n"
"FRate 449.37 396.19 2.57 2.38 206.80\n"
"Retry# 01 \n\n"
"--------- #1160 11/06/2015 09:47:58 Total: 2581.8 ---------\n"
"RCPE: 101 ID: 204 WKOD: 0 OPRT: 0 TARE: 12.7\n"
"MAT ADDI(2) REGR(4) ADDI(5) ADDI(6) NATU(8)\n"
" 2% 25% 0.5% 1.3% 100 \n"
"FINA R 1.91 25.06 0.49 1.30 100.00\n"
"FINA W 35.6 646.9 9.1 24.3 1865.9\n"
"1st DW 23.8 646.9 7.5 24.3 1865.9\n"
"1st DT 83.0 1578.0 3636.0 10188.0 8633.0\n"
"1st FR 449.37 396.19 2.57 2.38 206.80\n"
" DW/DT 287.02 409.98 2.07 2.38 216.13\n"
"FRate 449.37 396.19 2.32 2.38 211.47\n"
"Retry# 02 01 \n\n"
"--------- #1161 11/06/2015 09:49:01 Total: 2645.1 ---------\n"
"RCPE: 101 ID: 204 WKOD: 0 OPRT: 0 TARE: 12.3\n"
"MAT ADDI(2) REGR(4) ADDI(5) ADDI(6) NATU(8)\n"
" 2% 25% 0.5% 1.3% 100 \n"
"FINA R 1.87 24.36 0.52 1.34 100.00\n"
"FINA W 36.1 644.3 10.1 25.9 1928.8\n"
"1st DW 24.8 644.3 10.1 25.9 1928.8\n"
"1st DT 86.0 1578.0 4159.0 10532.0 8454.0\n"
"1st FR 449.37 396.19 2.32 2.38 211.47\n"
" DW/DT 288.18 408.28 2.43 2.46 228.15\n"
"FRate 449.37 396.19 2.32 2.42 219.81\n"
"Retry# 02 ")
matches = re.finditer(regex, test_str, re.VERBOSE)
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