# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(?<=<!-- Template Nav Start -->[\n])([\n\s\S]*?)(?=[\s\n]*?<!-- Template Nav End -->)"
test_str = ("<!DOCTYPE html>\n"
"<html lang=\"en\">\n\n"
" <head>\n"
" <meta charset=\"UTF-8\">\n"
" <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">\n"
" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n"
" <meta property=\"og:site_name\" content=\"WD Proposal\">\n"
" <meta property=\"og:title\" content=\"Your new outdoor living space.\">\n"
" <meta property=\"og:image\" content=\"https://www.WhitmerDecks.com/Proposal/eProposal/img/wdLogoName.png\">\n"
" <meta property=\"og:description\" content=\"Here you can view the proposal from Whitmer Decks, LLC\">\n"
" </meta>\n\n"
" <!-- General scripts & styles -->\n"
" <link rel=\"stylesheet\" href=\"css/styles.css\">\n"
" <link rel=\"stylesheet\" href=\"css/cwBootstrap.css\">\n"
" <script src=\"js/cwBootstrap.js\" defer></script>\n\n"
" <!-- Google Fonts -->\n"
" <link href=\"https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap\" rel=\"stylesheet\">\n\n"
" <!-- Page title and end of general head -->\n"
" <title>WD Proposal</title>\n\n"
" <!-- Page specific scripts and styles -->\n"
" <script src=\"js/about.js\" defer></script>\n"
" </head>\n\n"
" <body>\n\n"
" <!-- Template Nav Start -->\n"
" <nav>\n"
" <ul>\n"
" <li><a href=\"index.html\">Home</a></li>\n"
" <li><a href=\"about.html\">About</a></li>\n"
" <li>Item X</li>\n"
" <li>Item Y</li>\n"
" </ul>\n"
" </nav>\n"
" <!-- Template Nav End -->\n\n"
" <p>\n"
" The about page\n"
" </p>\n\n"
" </body>\n\n"
"</html>")
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