import re
regex = re.compile(r"(?<=<[Ss][Cc][Rr][Ii][Pp][Tt]>)([\s\S]*?)(?=<\/[Ss][Cc][Rr][Ii][Pp][Tt]>)")
test_str = ("<script>test</script>\n\n"
"<SCRIPT>\n"
"Example\n"
"</SCRIPT>\n\n"
" <script>\n"
" (function() {\n"
" var widget_id = 825946;\n"
" _shcp = [{\n"
" widget_id: widget_id\n"
" }];\n"
" var lang = (navigator.language || navigator.systemLanguage || navigator.userLanguage || \"en\")\n"
" .substr(0, 2).toLowerCase();\n"
" var url = \"widget.siteheart.com/widget/sh/\" + widget_id + \"/\" + lang + \"/widget.js\";\n"
" var hcc = document.createElement(\"script\");\n"
" hcc.type = \"text/javascript\";\n"
" hcc.async = true;\n"
" hcc.src = (\"https:\" == document.location.protocol ? \"https\" : \"http\") + \"://\" + url;\n"
" var s = document.getElementsByTagName(\"script\")[0];\n"
" s.parentNode.insertBefore(hcc, s.nextSibling);\n"
" })();\n"
" </script>")
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