# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"\<(ol)(?: .*?)?\>(?:[^\<]|\<(.*?)\>.*\<\/\2\>)*\<\/\1\>"
test_str = ("<ol>kjkjkj\n\n"
"</ol>\n\n\n\n"
"<ol s>\n"
" <li>Table of Contents.....................CT.TS</li>\n"
" <li>Gamecube Controls.....................CN.GC</li>\n"
" <li>Nintendo 64 Controls..................CN.64</li>\n"
" <li>Walkthrough...........................01.00\n"
" <ol>\n"
" <li>Kokiri Forest...............01.01</li>\n"
" </ol>\n"
" </li> \n"
" <li>Sidequests & Minigames................02.00\n"
" <ol>\n"
" <li>Shooting Gallery............02.01</li>\n"
" </ol>\n"
" </li>\n"
" <li>Boss Guide............................03.00\n"
" <ol>\n"
" <li>Gohma.......................03.01</li>\n"
" <li>Final Boss [Second Form]....03.15</li>\n"
" </ol>\n"
" </li>\n"
" <li>Item Checklist........................04.00\n"
" <ol>\n"
" <li>Final Dungeon...............04.47</li>\n"
" </ol>\n"
" </li>\n"
" <li>Shop Inventory........................05.00 \n"
" <ol>\n"
" <li>Kokiri Shop [Young].........05.01</li>\n"
" <li>Goron Shop [Adult]..........05.09</li>\n"
" </ol>\n"
" </li>\n"
" <li>Ocarina Notes.........................06.00\n"
" <ol><ol>sdsds</ol>\n"
" <li>Requ<ol></ol>iem of Spirit...........06.12</li>\n"
" </ol>\n"
" </li>\n"
" <li>Heart Containers......................07.00</li>\n"
" <li>Item List.............................15.00</li>\n"
" <li>Legal & Copyright.....................LE.AL</li>\n"
" <li>Credits & Thanks......................CR.DS</li>\n"
" </ol>\n\n"
"<ol><ol><ol><ol><ol></ol></ol></ol></ol></ol>\n\n\n"
"sdsd\n"
"k<ol>jlkjlkj \n"
"kjbkjh</ol> ")
matches = re.finditer(regex, test_str, re.DOTALL)
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