# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(?<wholekey>(?s)^\[.*?\].*?(?=^\[|\z))"
test_str = ("Windows Registry Editor Version 5.00\n\n"
"[HKEY_LOCAL_MACHINE]\n"
"\"ServiceLastKnownStatus\"=dword:00000002\n\n"
"[HKEY_LOCAL_MACHINE\\BCD00000000]\n\n"
"[HKEY_CURRENT_USER\\AppEvents\\Schemes\\Apps\\.Default\\Notification.Default\\.Current]\n"
"@=\"C:\\\\Windows\\\\media\\\\Windows Notify System Generic.wav\"\n\n"
"[HKEY_LOCAL_MACHINE\\BCD00000000\\Description]\n"
"\"KeyName\"=\"BCD00000000\"\n"
"\"TreatAsSystem\"=dword:00000001\n"
"\"GuidCache\"=hex:f4,60,9c,00,7b,c3,da,01,03,27,00,00,8a,d6,94,57,42,4c,7f,29,8a,\\\n"
" 00,00,00\n"
"\"Element\"=hex:00\n"
"\"Element\"=hex(7):7b,00,37,00,65,00,61,00,32,00,65,00,31,00,61,00,63,00,2d,00,\\\n"
" 32,00,65,00,36,00,31,00,2d,00,34,00,37,00,32,00,38,00,2d,00,61,00,61,00,61,\\\n"
" 00,33,00,2d,00,38,00,39,00,36,00,64,00,39,00,64,00,30,00,61,00,39,00,66,00,\\\n"
" 30,00,65,00,7d,00,00,00,00,00\n"
"\"Type\"=dword:101fffff\n"
"\"Element\"=\"EFI PXE 0 for IPv4 (74-5D-22-21-B1-95) \"\n"
"\"OSMaxVersionTested\"=hex(b):00,00,ff,3f,00,00,0a,00\n"
"\"OSMinVersion\"=hex(b):00,00,00,00,00,00,0a,00\n"
"\"WMP11.AssocFile.3G2\"=hex(0):\n"
"\"Drivers\"=hex(100000):\n"
"\"DriverType\"=hex(200000):\n"
"\"lParam\"=hex(4):00,00,00,00,00,00,00,00\n\n"
"[HKEY_LOCAL_MACHINE\\BCD00000000\\Objects\\{3edef9e6-2f6e-11ef-968a-9457424c7f29}\\Elements]\n\n"
"[HKEY_LOCAL_MACHINE\\BCD00000000\\Objects\\{3edef9e9-2f6e-11ef-968a-9457424c7f29}]\n"
"\"command\"=hex(2):22,00,25,00,70,00,72,00,6f,00,67,00,72,00,61,00,6d,00,46,00,\\\n"
" 69,00,6c,00,65,00,73,00,25,00,5c,00,57,00,69,00,6e,00,64,00,6f,00,77,00,73,\\\n"
" 00,20,00,4d,00,61,00,69,00,6c,00,5c,00,57,00,61,00,62,00,2e,00,65,00,78,00,\\\n"
" 65,00,22,00,20,00,2f,00,43,00,72,00,65,00,61,00,74,00,65,00,43,00,6f,00,6e,\\\n"
" 00,74,00,61,00,63,00,74,00,20,00,22,00,25,00,31,00,22,00,00,00\n"
"\"PrimaryLineOutTopo\"=hex(40007):41,00,70,00,6f,00,50,00,72,00,65,00,73,00,65,\\\n"
" 00,74,00,31,00,00,00,00,00\n"
"\"Element\"=hex(7):7b,00,31,00,61,00,66,00,61,00,39,00,63,00,34,00,39,00,2d,00,\\\n"
" 31,00,36,00,61,00,62,00,2d,00,34,00,61,00,35,00,63,00,2d,00,39,00,30,00,31,\\\n"
" 00,62,00,2d,00,32,00,31,00,32,00,38,00,30,00,32,00,64,00,61,00,39,00,34,00,\\\n"
" 36,00,30,00,7d,00,00,00,00,00\n"
"\"Isa\"=hex(8):01,00,00,00,01,00,00,00,00,00,00,00,00,00,00,00,28,00,00,00,01,01,\\\n"
" 00,00,00,00,00,00,00,00,00,00,00,01,00,00,01,03,00,00,e8,42,00,00,00,00,00,\\\n"
" 00,08,00,00,00,01,03,00,00,e8,4a,00,00,00,00,00,00,08,00,00,00,01,03,00,00,\\\n"
" e8,82,00,00,00,00,00,00,08,00,00,00,01,03,00,00,e8,86,00,00,00,00,00,00,08,\\\n"
" 00,00,00,01,03,00,00,e8,8a,00,00,00,00,00,00,08,00,00,00,01,03,00,00,e8,8e,\\\n"
" 00,00,00,00,00,00,08,00,00,00,01,03,00,00,e8,92,00,00,00,00,00,00,08,00,00,\\\n"
" 00,01,03,00,00,e8,96,00,00,00,00,00,00,08,00,00,00,01,03,00,00,e8,9a,00,00,\\\n"
" 00,00,00,00,08,00,00,00,01,03,00,00,e8,9e,00,00,00,00,00,00,08,00,00,00,01,\\\n"
" 03,00,00,e8,a2,00,00,00,00,00,00,08,00,00,00,01,03,00,00,e8,a6,00,00,00,00,\\\n"
" 00,00,08,00,00,00,01,03,00,00,e8,aa,00,00,00,00,00,00,08,00,00,00,01,03,00,\\\n"
" 00,e8,ae,00,00,00,00,00,00,08,00,00,00,01,03,00,00,e8,b6,00,00,00,00,00,00,\\\n"
" 08,00,00,00,01,03,00,00,e8,ba,00,00,00,00,00,00,08,00,00,00,01,03,00,00,e8,\\\n"
" be,00,00,00,00,00,00,08,00,00,00,01,03,00,00,e8,c2,00,00,00,00,00,00,08,00,\\\n"
" 00,00,01,03,00,00,e8,c6,00,00,00,00,00,00,08,00,00,00,01,03,00,00,e8,ca,00,\\\n"
" 00,00,00,00,00,08,00,00,00,01,03,00,00,e8,ce,00,00,00,00,00,00,08,00,00,00,\\\n"
" 01,03,00,00,e8,d2,00,00,00,00,00,00,08,00,00,00,01,03,00,00,e8,d6,00,00,00,\\\n"
" 00,00,00,08,00,00,00,01,03,00,00,e8,da,00,00,00,00,00,00,08,00,00,00,01,03,\\\n"
" 00,00,e8,de,00,00,00,00,00,00,08,00,00,00,01,03,00,00,e8,e2,00,00,00,00,00,\\\n"
" 00,08,00,00,00,01,03,00,00,e8,e6,00,00,00,00,00,00,08,00,00,00,01,03,00,00,\\\n"
" e8,ea,00,00,00,00,00,00,08,00,00,00,01,03,00,00,e8,ee,00,00,00,00,00,00,08,\\\n"
" 00,00,00,01,03,00,00,ee,f6,00,00,00,00,00,00,02,00,00,00,01,03,00,00,ee,fa,\\\n"
" 00,00,00,00,00,00,02,00,00,00,01,03,00,00,ee,fe,00,00,00,00,00,00,02,00,00,\\\n"
" 00,02,03,00,00,03,00,00,00,03,00,00,00,ff,ff,ff,ff,02,03,00,00,04,00,00,00,\\\n"
" 04,00,00,00,ff,ff,ff,ff,02,03,00,00,0e,00,00,00,0e,00,00,00,ff,ff,ff,ff,02,\\\n"
" 03,00,00,06,00,00,00,06,00,00,00,ff,ff,ff,ff,02,03,00,00,0c,00,00,00,0c,00,\\\n"
" 00,00,ff,ff,ff,ff,02,03,00,00,01,00,00,00,01,00,00,00,ff,ff,ff,ff,03,03,00,\\\n"
" 00,ff,ff,bf,ff,00,00,00,00,00,00,40,00\n"
"\"Configuration Data\"=hex(9):ff,ff,ff,ff,ff,ff,ff,ff,00,00,00,00,02,00,00,00,05,\\\n"
" 00,00,00,0c,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,ff,ff,ff,ff,\\\n"
" 00,00,ff,ff,01,00,05,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,\\\n"
" 00\n"
"\"Pci\"=hex(a):e8,01,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,\\\n"
" 00,00,00,00,00,00,01,00,00,00,01,00,01,00,0e,00,00,00,00,01,00,00,00,00,00,\\\n"
" 00,00,00,00,00,00,00,00,00,00,00,01,00,00,00,00,00,ff,ff,ff,ff,00,00,00,00,\\\n"
" 08,01,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,05,00,00,00,00,00,00,ff,\\\n"
" ff,00,00,00,00,00,00,08,01,00,00,00,00,00,00,00,00,00,00,00,00,00,00,40,01,\\\n"
" 00,00,00,00,00,00,7f,01,00,00,00,00,00,00,08,01,00,00,00,00,00,00,00,00,00,\\\n"
" 00,00,00,00,00,00,02,00,00,00,00,00,00,ff,02,00,00,00,00,00,00,08,01,00,00,\\\n"
" 00,00,00,00,00,00,00,00,00,00,00,00,00,03,00,00,00,00,00,00,6f,03,00,00,00,\\\n"
" 00,00,00,08,01,00,00,00,00,00,00,00,00,00,00,00,00,00,00,78,03,00,00,00,00,\\\n"
" 00,00,7a,03,00,00,00,00,00,00,08,01,00,00,00,00,00,00,00,00,00,00,00,00,00,\\\n"
" 00,e8,02,00,00,00,00,00,00,ff,02,00,00,00,00,00,00,08,01,00,00,00,00,00,00,\\\n"
" 00,00,00,00,00,00,00,00,f0,01,00,00,00,00,00,00,f8,01,00,00,00,00,00,00,08,\\\n"
" 01,00,00,00,00,00,00,00,00,00,00,00,00,00,00,b0,03,00,00,00,00,00,00,cf,03,\\\n"
" 00,00,00,00,00,00,08,01,00,00,00,00,00,00,00,00,00,00,00,00,00,00,e8,03,00,\\\n"
" 00,00,00,00,00,ff,03,00,00,00,00,00,00,08,01,00,00,00,00,00,00,00,00,00,00,\\\n"
" 00,00,00,00,ce,01,00,00,00,00,00,00,cf,01,00,00,00,00,00,00,08,01,00,00,00,\\\n"
" 00,00,00,00,00,00,00,00,00,00,00,00,01,00,00,00,00,00,00,ff,03,00,00,00,00,\\\n"
" 00,00,00,03,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,10,00,00,00,00,\\\n"
" 00,ff,ff,ff,ff,ff,ff,ff,ff,08,03,00,00,00,00,00,00,00,00,00,00,00,00,00,00,\\\n"
" 00,00,08,00,00,00,00,00,ff,ff,0d,00,00,00,00,00")
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