# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"^\s(\w{1,}):\s+((?:\w\w\s)+)\s+(\w+)(.*)"
test_str = (" 6bb: c3 ret \n"
" 6bc: 0f 1f 40 00 nop DWORD PTR [rax+0x0]\n"
" 6c0: 41 57 push r15\n"
" 6c2: 41 56 push r14\n"
" 6c4: 41 89 ff mov r15d,edi\n"
" 6c7: 41 55 push r13\n"
" 6c9: 41 54 push r12\n"
" 6cb: 4c 8d 25 06 07 20 00 lea r12,[rip+0x200706] # 0x200dd8\n"
" 6d2: 55 push rbp\n"
" 6d3: 48 8d 2d 06 07 20 00 lea rbp,[rip+0x200706] # 0x200de0\n"
" 6da: 53 push rbx\n"
" 6db: 49 89 f6 mov r14,rsi\n"
" 6de: 49 89 d5 mov r13,rdx\n"
" 6e1: 4c 29 e5 sub rbp,r12\n"
" 6e4: 48 83 ec 08 sub rsp,0x8\n"
" 6e8: 48 c1 fd 03 sar rbp,0x3\n"
" 6ec: e8 3f fe ff ff call 0x530\n"
" 6f1: 48 85 ed test rbp,rbp\n"
" 6f4: 74 20 je 0x716\n"
" 6f6: 31 db xor ebx,ebx\n"
" 6f8: 0f 1f 84 00 00 00 00 nop DWORD PTR [rax+rax*1+0x0]\n"
" 6ff: 00 \n"
" 700: 4c 89 ea mov rdx,r13\n"
" 703: 4c 89 f6 mov rsi,r14\n"
" 706: 44 89 ff mov edi,r15d\n"
" 709: 41 ff 14 dc call QWORD PTR [r12+rbx*8]\n"
" 70d: 48 83 c3 01 add rbx,0x1\n"
" 711: 48 39 dd cmp rbp,rbx\n"
" 714: 75 ea jne 0x700\n"
" 716: 48 83 c4 08 add rsp,0x8\n"
" 71a: 5b pop rbx\n"
" 71b: 5d pop rbp\n"
" 71c: 41 5c pop r12\n"
" 71e: 41 5d pop r13\n"
" 720: 41 5e pop r14\n"
" 722: 41 5f pop r15\n"
" 724: c3 ret \n"
" 725: 90 nop\n"
" 726: 66 2e 0f 1f 84 00 00 nop WORD PTR cs:[rax+rax*1+0x0]\n"
" 72d: 00 00 00 \n"
" 730: f3 c3 repz ret \n")
matches = re.finditer(regex, test_str, re.MULTILINE | 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