# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"<\s*[a-z]*[^>]*>(.*?)<\s*/\s*[a-z]*>|<[\w]*>(.*?)<[/\w]*>"
test_str = ("<body>\n\n"
"<div class=\"header\">\n"
" <h1>My Website</h1>\n"
" <p>A <b>responsive</b> website created by me.</p>\n"
"</div>\n\n"
"<div class=\"navbar\">\n"
" <a href=\"#\" class=\"active\">Home</a>\n"
" <a href=\"#\">Link</a>\n"
" <a href=\"#\">Link</a>\n"
" <a href=\"#\" class=\"right\">Link</a>\n"
"</div>\n\n"
"<div class=\"row\">\n"
" <div class=\"side\">\n"
" <h2>About Me</h2>\n"
" <h5>Photo of me:</h5>\n"
" <div class=\"fakeimg\" style=\"height:200px;\">Image</div>\n"
" <p>Some text about me in culpa qui officia deserunt mollit anim..</p>\n"
" <h3>More Text</h3>\n"
" <p>Lorem ipsum dolor sit ame.</p>\n"
" <div class=\"fakeimg\" style=\"height:60px;\">Image</div><br>\n"
" <div class=\"fakeimg\" style=\"height:60px;\">Image</div><br>\n"
" <div class=\"fakeimg\" style=\"height:60px;\">Image</div>\n"
" </div>\n"
" <div class=\"main\">\n"
" <h2>TITLE HEADING</h2>\n"
" <h5>Title description, Dec 7, 2017</h5>\n"
" <div class=\"fakeimg\" style=\"height:200px;\">Image</div>\n"
" <p>Some text..</p>\n"
" <p>Sunt in culpa qui officia deserunt mollit anim id est laborum consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco.</p>\n"
" <br>\n"
" <h2>TITLE HEADING</h2>\n"
" <h5>Title description, Sep 2, 2017</h5>\n"
" <div class=\"fakeimg\" style=\"height:200px;\">Image</div>\n"
" <p>Some text..</p>\n"
" <p>Sunt in culpa qui officia deserunt mollit anim id est laborum consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco.</p>\n"
" </div>\n"
"</div>\n\n"
"<div class=\"footer\">\n"
" <h2>Footer</h2>\n"
"</div>\n\n"
"</body>")
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