import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?<=<[Ss][Cc][Rr][Ii][Pp][Tt]>)([\\s\\S]*?)(?=<\\/[Ss][Cc][Rr][Ii][Pp][Tt]>)";
final String string = "<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>";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}
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 Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html