import re
regex = re.compile(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 = regex.finditer(test_str)
for match_num, match in enumerate(matches, start=1):
print(f"Match {match_num} was found at {match.start()}-{match.end()}: {match.group()}")
for group_num, group in enumerate(match.groups(), start=1):
print(f"Group {group_num} found at {match.start(group_num)}-{match.end(group_num)}: {group}")
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