import re
regex = re.compile(r"<!-- build-remove-start -->([^<]*(?:<(?!!-- build-remove-end -->)[^<]*)*)<!-- build-remove-end -->")
test_str = ("<!DOCTYPE html>\n"
"<html lang=\"en\">\n"
"<head>\n"
" <meta charset=\"UTF-8\">\n"
" <title>Hello</title>\n"
" <!-- bower:css -->\n"
" <!-- endbower -->\n\n"
" <!-- inject:css -->\n"
" <!-- endinject -->\n"
"</head>\n"
"<body>\n"
"<h1>Hello World!</h1>\n\n"
"<!-- build-remove-start -->\n"
"<!-- bower:js -->\n"
"<script src=\"../bower_components/jquery/dist/jquery.js\"></script>\n"
"<!-- endbower -->\n"
"<!-- build-remove-end -->\n\n"
"<!-- inject:vendor:js -->\n"
"<!-- endinject -->\n\n\n"
"<!-- inject:js -->\n"
"<!-- endinject -->\n\n"
"<!-- inject:app:js -->\n"
"<!-- endinject -->\n"
"</body>\n"
"</html>")
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