# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"\s{2,3}(\d+)\|\s{8,9}(\w+)\|\s{3}(ON\s|OFF)\|(\s+|\s+\w+)\|\s{5}(ON\s|OFF)\n|\s{3}(\d)\s\|\s+(\w+)\s+\|\s+(ON\s|OFF)\|(\s{2}\n|.\w+)|AC current draw\s(\d)\:\s(\d\.\d{2})|Max detected\s(\d\.\d{2})"
test_str = ("pshow\n\n\n\n"
"Port | Name |Status| Reservation\n\n"
" 1 | Fan | ON | \n\n"
" 2 | Clim | OFF| mofa\n\n\n"
">\n\n"
"����������\"\n"
"Synaccess Inc. Telnet Session V6.2.\n\n\n"
"pshow\n\n"
">\n\n\n"
"Port| Name |status| Reservation |RB Timer\n\n"
"----+----------------+------+----------------+--------\n\n"
" 1| Outlet1| OFF| | ON \n\n"
" 2| Outlet2| ON | mofa| OFF\n\n"
" 3| Outlet3| ON | | OFF\n\n"
" 4| Outlet4| OFF| | OFF\n\n"
" 5| Outlet5| ON | | OFF\n\n"
" 6| Outlet6| ON | | OFF\n\n"
" 7| Outlet7| OFF| | OFF\n\n"
" 8| Outlet8| ON | | OFF\n\n"
" 9| Outlet9| ON | | OFF\n\n"
" 10| Outlet10| OFF| | OFF\n\n\n"
"AC current draw 1: 0.05. Max detected 0.78 Amps.\n\n\n"
"Temperature Probe is not installed\n"
">")
matches = re.finditer(regex, test_str)
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