# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(?im)^[^\S\r\n]*([^()\r\n]+?)[^\S\r\n]*(?:(\(.*?\)))?[^\S\r\n]*\(([a-f0-9]+(?:-[a-f0-9]+)*)\)[^\S\r\n]*(\(.*?\))"
test_str = ("iPhone 5s (B6780382-ADBC-4F92-B1CA-52111A5A6FBE) (Shutdown)\n"
"iPhone 6 (FDA15891-2487-4C9E-A7F2-0E476C652E1D) (Shutdown)\n"
"iPhone 6 Plus (58D4FC86-8E22-4995-816D-BC8799AE1220) (Shutdown)\n"
"iPhone 6s (AE9BCF92-4814-4ABD-A791-FA60E0919156) (Shutdown)\n"
"iPhone 6s Plus (5D431BB6-42AE-455D-8501-2227687D1A00) (Shutdown)\n"
"iPhone 7 (0211256D-308F-4210-807D-DAAD02CC8AD9) (Shutdown)\n"
"iPhone 7 Plus (872D5513-434C-4618-9350-A2AB77B04459) (Shutdown)\n"
"iPhone 8 (74F7E423-1D17-475E-B18B-0FAAFC19DD0F) (Shutdown)\n"
"iPhone 8 Plus (DB424FB3-6D34-4FE9-82AD-E50F439A47AD) (Shutdown)\n"
"iPhone SE (ECF43B3D-38E1-4A37-8B07-C3CB72B14AE0) (Shutdown)\n"
"iPhone X (40D45896-6325-4BB6-99D2-420E3472B68E) (Shutdown)\n"
"iPhone Xs (5AAFEA8B-CE48-4DBE-9BAD-F9A714FAB4BA) (Shutdown)\n"
"iPhone Xs Max (29946005-C6FF-4E77-98C4-A104917A9FD9) (Shutdown)\n"
"iPhone Xʀ (205A5924-472E-4B83-AB7D-5FA56D93DC01) (Shutdown)\n"
"iPad Air (3rd generation) (54CE7050-9D80-4000-87AC-D1717C3F5890) (Shutdown)\n"
"iPad Air (83D98641-4FE8-476A-991E-14C0BF687DC6) (Shutdown)\n"
"iPad Air 2 (E6951B0F-2DD3-4361-8469-BA319D1FACA9) (Shutdown)\n"
"iPad (5th generation) (CB7A9551-9B29-4999-82A0-872C3700C316) (Shutdown)\n"
"iPad Pro (9.7-inch) (98FEC483-DC9B-4092-896E-CB9E8F165E51) (Shutdown)\n"
"iPad Pro (12.9-inch) (DD14F482-E456-4E6D-A044-C72818A7C52D) (Shutdown)\n"
"iPad Pro (12.9-inch) (2nd generation) (8C8364BA-A85D-4B3D-AD30-B179CA7F4541) (Shutdown)\n"
"iPad Pro (10.5-inch) (AA6DEE43-167D-42A4-9399-4727651BDAC7) (Shutdown)\n"
"iPad (6th generation) (040A1950-770F-4DB0-BC0A-91237C23AAC6) (Shutdown)\n"
"iPad Pro (11-inch) (07D97297-2E42-4FE8-B105-3245737D0CA3) (Shutdown)\n"
"iPad Pro (12.9-inch) (3rd generation) (692172E8-2AFB-465A-AC0D-4109AF1679B5) (Shutdown)\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