# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(Device\s#\d(\n.*)*?(?=\n\s*Device\s#|\Z))"
test_str = (" Controllers found: 1\n"
"----------------------------------------------------------------------\n"
"Physical Device information\n"
"----------------------------------------------------------------------\n"
" Device #0\n"
" Device is a Hard drive\n"
" State : Online\n"
" Supported : Yes\n"
" Transfer Speed : SAS 3.0 Gb/s\n"
" Reported Channel,Device(T:L) : 0,0(0:0)\n"
" Reported Location : Enclosure 0, Slot 31\n"
" Reported ESD(T:L) : 2,0(0:0)\n"
" Vendor : HP\n"
" Model : DG072A4951\n"
" Firmware : HPDB\n"
" Serial number : P2VDJTUA\n"
" World-wide name : 5000CCA00016CE17\n"
" Reserved Size : 109705 KB\n"
" Used Size : 69900 MB\n"
" Unused Size : 64 KB\n"
" Total Size : 70007 MB\n"
" Write Cache : Enabled (write-back)\n"
" FRU : None\n"
" S.M.A.R.T. : No\n"
" S.M.A.R.T. warnings : 0\n"
" Power State : Full rpm\n"
" Supported Power States : Full rpm,Powered off\n"
" SSD : No\n"
" Device #1\n"
" Device is a Hard drive\n"
" State : Online\n"
" Supported : Yes\n"
" Transfer Speed : SAS 3.0 Gb/s\n"
" Reported Channel,Device(T:L) : 0,1(1:0)\n"
" Reported Location : Enclosure 0, Slot 30\n"
" Reported ESD(T:L) : 2,0(0:0)\n"
" Vendor : HP\n"
" Model : DG072A4951\n"
" Firmware : HPDB\n"
" Serial number : P2VDLG6A\n"
" World-wide name : 5000CCA00016E6FB\n"
" Reserved Size : 109705 KB\n"
" Used Size : 69900 MB\n"
" Unused Size : 64 KB\n"
" Total Size : 70007 MB\n"
" Write Cache : Enabled (write-back)\n"
" FRU : None\n"
" S.M.A.R.T. : No\n"
" S.M.A.R.T. warnings : 0\n"
" Power State : Full rpm\n"
" Supported Power States : Full rpm,Powered off\n"
" SSD : No\n"
" Device #2\n"
" Device is a Hard drive\n"
" State : Online\n"
" Supported : Yes\n"
" Transfer Speed : SAS 6.0 Gb/s\n"
" Reported Channel,Device(T:L) : 0,2(2:0)\n"
" Reported Location : Enclosure 0, Slot 29\n"
" Reported ESD(T:L) : 2,0(0:0)\n"
" Vendor : HP\n"
" Model : EG0300FAWHV\n"
" Firmware : HPDE\n"
" Serial number : 6SE4RLA90000B206N27S\n"
" World-wide name : 5000C500429DA878\n"
" Reserved Size : 415982 KB\n"
" Used Size : 285696 MB\n"
" Unused Size : 64 KB\n"
" Total Size : 286102 MB\n"
" Write Cache : Enabled (write-back)\n"
" FRU : None\n"
" S.M.A.R.T. : No\n"
" S.M.A.R.T. warnings : 0\n"
" Power State : Full rpm\n"
" Supported Power States : Full rpm,Powered off\n"
" SSD : No\n"
" Device #3\n"
" Device is a Hard drive\n"
" State : Online\n"
" Supported : Yes\n"
" Transfer Speed : SAS 6.0 Gb/s\n"
" Reported Channel,Device(T:L) : 0,3(3:0)\n"
" Reported Location : Enclosure 0, Slot 28\n"
" Reported ESD(T:L) : 2,0(0:0)\n"
" Vendor : HP\n"
" Model : EG0300FAWHV\n"
" Firmware : HPDE\n"
" Serial number : 6SE4NYMY0000B20416MP\n"
" World-wide name : 5000C50042999500\n"
" Reserved Size : 415982 KB\n"
" Used Size : 285696 MB\n"
" Unused Size : 64 KB\n"
" Total Size : 286102 MB\n"
" Write Cache : Enabled (write-back)\n"
" FRU : None\n"
" S.M.A.R.T. : No\n"
" S.M.A.R.T. warnings : 0\n"
" Power State : Full rpm\n"
" Supported Power States : Full rpm,Powered off\n"
" SSD : No")
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