import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?><(?:(?:(?:(script|style|object|embed|applet|noframes|noscript|noembed)(?:\\s+(?>\"[\\S\\s]*?\"|'[\\S\\s]*?'|(?:(?!\\/>)[^>])?)+)?\\s*>)[\\S\\s]*?<\\/\\1\\s*(?=>))|(?:\\/?(?!body)[\\w:]+\\s*\\/?)|(?:(?!body)[\\w:]+\\s+(?:\"[\\S\\s]*?\"|'[\\S\\s]*?'|[^>]?)+\\s*\\/?)|\\?[\\S\\s]*?\\?|(?:!(?:(?:DOCTYPE[\\S\\s]*?)|(?:\\[CDATA\\[[\\S\\s]*?\\]\\])|(?:--[\\S\\s]*?--)|(?:ATTLIST[\\S\\s]*?)|(?:ENTITY[\\S\\s]*?)|(?:ELEMENT[\\S\\s]*?))))>|[\\S\\s])*?<body(?:\\s+(?>\"[\\S\\s]*?\"|'[\\S\\s]*?'|(?:(?!\\/>)[^>])?)+)?\\s*>((?:<(?:(?:(?:(script|style|object|embed|applet|noframes|noscript|noembed)(?:\\s+(?>\"[\\S\\s]*?\"|'[\\S\\s]*?'|(?:(?!\\/>)[^>])?)+)?\\s*>)[\\S\\s]*?<\\/\\3\\s*(?=>))|(?:\\/?(?!body)[\\w:]+\\s*\\/?)|(?:(?!body)[\\w:]+\\s+(?:\"[\\S\\s]*?\"|'[\\S\\s]*?'|[^>]?)+\\s*\\/?)|\\?[\\S\\s]*?\\?|(?:!(?:(?:DOCTYPE[\\S\\s]*?)|(?:\\[CDATA\\[[\\S\\s]*?\\]\\])|(?:--[\\S\\s]*?--)|(?:ATTLIST[\\S\\s]*?)|(?:ENTITY[\\S\\s]*?)|(?:ELEMENT[\\S\\s]*?))))>|[\\S\\s])*)<\\/body\\s*>";
final String string = "<html>\n"
+ " <head>\n\n"
+ " <meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">\n"
+ " </head>\n"
+ " <!-- <body class=\"blah\">original mock body </body> -->\n"
+ " <body text=\"#000000\" bgcolor=\"#FFFFFF\">\n"
+ "<!CDATA[Raw unparsed character data can contain </body> with no problems!]]>\n"
+ "<!-- Comments can also contain </body> -->\n"
+ "So can JScript\n"
+ "<script>/*<![CDATA[*/window.jQuery && jQuery.ready();/*]]>*/</script><script>if(window.mw){\n"
+ "mw.loader.state({\"ext.glo</body>balCssJs.site\":\"ready\",\"ext.globalCssJs.user\":\"ready\",\"site\":\"loading\",\"user\":\"ready\",\"user.groups\":\"ready\"});\n"
+ "}</script>\n"
+ " <p>dpioaushd iuashdiu ashd</p>\n"
+ " <p> has</p>\n"
+ " <p>ud ashuod sh</p>\n"
+ " <p>odu sad ha</p>\n"
+ " <p>suod sh</p>\n"
+ " <p>od uashod uahd<br>\n"
+ " </p>\n"
+ " <div class=\"moz-signature\">-- <br>\n"
+ " <img src=\"cid:part1.8C289150.C3F89C42@wssim.com.br\" border=\"0\"></div>\n"
+ " </body>\n"
+ "</html>";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
if (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