# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(\w*\(Hex\): w*)(.*?)(?= |$)"
test_str = (" Vserver Name: vs1\n"
" LUN Path: /vol/vol1/lun1\n"
" Volume Name: vol1\n"
" Qtree Name: \"\"\n"
" LUN Name: lun1\n"
" LUN Size: 10MB\n"
" OS Type: linux\n"
" Space Reservation: disabled\n"
" Serial Number: wCVt1]IlvQWv\n"
" Serial Number (Hex): 77435674315d496c76515776\n"
" Comment: new comment\n"
"Space Reservations Honored: false\n"
" Space Allocation: disabled\n"
" State: offline\n"
" LUN UUID: 76d2eba4-dd3f-494c-ad63-1995c1574753\n"
" Mapped: mapped\n"
" Block Size: 512\n"
" Device Legacy ID: -\n"
" Device Binary ID: -\n"
" Device Text ID: -\n"
" Read Only: false\n"
" Fenced Due to Restore: false\n"
" Used Size: 5MB\n"
" Maximum Resize Size: 64.00GB\n"
" Creation Time: 9/14/2016 13:55:09\n"
" Class: regular\n"
" Node Hosting the LUN: node1\n"
" QoS Policy Group: -\n"
" Caching Policy Name: -\n"
" Clone: false\n"
" Clone Autodelete Enabled: false\n"
" Inconsistent Import: false\n"
" Application: -")
matches = re.finditer(regex, test_str, re.MULTILINE | re.DOTALL | re.IGNORECASE)
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