# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"^\s*(?<name>[^|]+?)\s*\|\s*(?<value>[^|]+?)\s*\|\s*(?<unit>[^|]+?)\s*\|\s*(?<status>[^|]+?)\s*\|\s*(?<type>[^|]+?)\s*\|\s*(?<state>[^|]+?)\s*\|\s*(?<lower_nonrec>[^|]+?)\s*\|\s*(?<lower_crit>[^|]+?)\s*\|\s*(?<lower_noncrit>[^|]+?)\s*\|\s*(?<upper_crit>[^|]+?)\s*$"
test_str = ("CPU1 Temp | 0x0 | discrete | 0x0000| na | na | na | na | na | na \n"
"CPU2 Temp | 0x0 | discrete | 0x0000| na | na | na | na | na | na \n"
"System Temp | 35,000 | degrees C | ok | -9,000 | -7,000 | -5,000 | 75,000 | 77,000 | 79,000 \n"
"CPU1 Vcore | 1,200 | Volts | ok | 0,656 | 0,664 | 0,672 | 1,352 | 1,360 | 1,368 \n"
"CPU2 Vcore | 0,936 | Volts | ok | 0,656 | 0,664 | 0,672 | 1,352 | 1,360 | 1,368 \n"
"CPU1 DIMM | 1,520 | Volts | ok | 1,184 | 1,192 | 1,200 | 1,648 | 1,656 | 1,664 \n"
"CPU2 DIMM | 1,520 | Volts | ok | 1,184 | 1,192 | 1,200 | 1,648 | 1,656 | 1,664 \n"
"+1.5V | 1,504 | Volts | ok | 1,320 | 1,328 | 1,336 | 1,656 | 1,664 | 1,672 \n"
"+3.3V | 3,240 | Volts | ok | 2,880 | 2,904 | 2,928 | 3,648 | 3,672 | 3,696 \n"
"+3.3VSB | 3,216 | Volts | ok | 2,880 | 2,904 | 2,928 | 3,648 | 3,672 | 3,696 \n"
"+5V | 5,056 | Volts | ok | 4,416 | 4,448 | 4,480 | 5,536 | 5,568 | 5,600 \n"
"+12V | 12,137 | Volts | ok | 10,600 | 10,653 | 10,706 | 13,250 | 13,303 | 13,356 \n"
"VBAT | 3,192 | Volts | ok | 2,880 | 2,904 | 2,928 | 3,648 | 3,672 | 3,696 \n"
"Fan1 | na | RPM | na | 405,000 | 540,000 | 675,000 | 34155,000 | 34290,000 | 34425,000 \n"
"Fan2 | na | RPM | na | 405,000 | 540,000 | 675,000 | 34155,000 | 34290,000 | 34425,000 \n"
"Fan3 | na | RPM | na | 405,000 | 540,000 | 675,000 | 34155,000 | 34290,000 | 34425,000 \n"
"Fan4 | na | RPM | na | 405,000 | 540,000 | 675,000 | 34155,000 | 34290,000 | 34425,000 \n"
"Fan5 | na | RPM | na | 405,000 | 540,000 | 675,000 | 34155,000 | 34290,000 | 34425,000 \n"
"Fan6 | na | RPM | na | 405,000 | 540,000 | 675,000 | 34155,000 | 34290,000 | 34425,000 \n"
"Fan7 | na | RPM | na | 405,000 | 540,000 | 675,000 | 34155,000 | 34290,000 | 34425,000 \n"
"Fan8 | na | RPM | na | 405,000 | 540,000 | 675,000 | 34155,000 | 34290,000 | 34425,000 \n"
"P1-DIMM1A Temp | 27,000 | degrees C | ok | -9,000 | -7,000 | -5,000 | 80,000 | 85,000 | 90,000 \n"
"P1-DIMM1B Temp | 26,000 | degrees C | ok | -9,000 | -7,000 | -5,000 | 80,000 | 85,000 | 90,000 \n"
"P1-DIMM2A Temp | 28,000 | degrees C | ok | -9,000 | -7,000 | -5,000 | 80,000 | 85,000 | 90,000 \n"
"P1-DIMM2B Temp | 28,000 | degrees C | ok | -9,000 | -7,000 | -5,000 | 80,000 | 85,000 | 90,000 \n"
"P1-DIMM3A Temp | 29,000 | degrees C | ok | -9,000 | -7,000 | -5,000 | 80,000 | 85,000 | 90,000 \n"
"P1-DIMM3B Temp | 29,000 | degrees C | ok | -9,000 | -7,000 | -5,000 | 80,000 | 85,000 | 90,000 \n"
"P2-DIMM1A Temp | 27,000 | degrees C | ok | -9,000 | -7,000 | -5,000 | 80,000 | 85,000 | 90,000 \n"
"P2-DIMM1B Temp | 26,000 | degrees C | ok | -9,000 | -7,000 | -5,000 | 80,000 | 85,000 | 90,000 \n"
"P2-DIMM2A Temp | 28,000 | degrees C | ok | -9,000 | -7,000 | -5,000 | 80,000 | 85,000 | 90,000 \n"
"P2-DIMM2B Temp | 28,000 | degrees C | ok | -9,000 | -7,000 | -5,000 | 80,000 | 85,000 | 90,000 \n"
"P2-DIMM3A Temp | 29,000 | degrees C | ok | -9,000 | -7,000 | -5,000 | 80,000 | 85,000 | 90,000 \n"
"P2-DIMM3B Temp | 29,000 | degrees C | ok | -9,000 | -7,000 | -5,000 | 80,000 | 85,000 | 90,000 \n"
"Intrusion | 0x1 | discrete | 0x0100| na | na | na | na | na | na \n"
"PS Status | 0x1 | discrete | 0x0100| na | na | na | na | na | na ")
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