import re
regex = re.compile(r"\bcontainer\W+(?:\w+\W+){1,6}?switcher\b")
test_str = ("<ul class=\"secondary\">\n"
"<li><a href=\"/all/\">all code snippets</a>/</li>\n"
"<li><a href=\"/popular/\">popular code snippets</a>/</li>\n"
"<li><a href=\"/yours/\">your code snippets</a></li>\n"
"</ul>\n"
"</li>\n"
"<li class=\"developer\">\n"
"<a href=\"/developer/\">extras</a>\n"
"</li>\n"
"<li class=\"blog\"><a href=\"/blog/\">blog</a></li>\n"
"<li class=\"about\"><a href=\"/about/\">about snipplr</a></li>\n"
"</ul>\n"
"</div>\n"
"</div>\n"
"</div>\n"
"<div id=\"subnav\">\n"
"<div class=\"container\">\n"
"<div id=\"switcher\">\n"
"<span>Change style:</span>\n"
"<ul>\n"
"<li><a class=\"styleswitch\" href=\"#\" onclick=\"setActiveStyleSheet('big'); return false;\">Big</a> / </li>\n"
"<li><a class=\"styleswitch\" href=\"#\" onclick=\"setActiveStyleSheet('small'); return false;\">Small</a></li>")
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