import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = ".*?(<\\/?\\w+).*?(\\/?>).*?";
final String string = "<!doctype html>\n"
+ "<html>\n"
+ "<head>\n"
+ "<title>Example Domain</title>\n\n"
+ "<meta charset=\"utf-8\" />\n"
+ "<meta http-equiv=\"Content-type\" content=\"text/html; charset=utf-8\" />\n"
+ "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n"
+ "<style type=\"text/css\">\n"
+ "body {\n"
+ " background-color: #f0f0f2;\n"
+ " margin: 0;\n"
+ " padding: 0;\n"
+ " font-family: \"Open Sans\", \"Helvetica Neue\", Helvetica, Arial, sans-serif;\n\n"
+ "}\n"
+ "div {\n"
+ " width: 600px;\n"
+ " margin: 5em auto;\n"
+ " padding: 50px;\n"
+ " background-color: #fff;\n"
+ " border-radius: 1em;\n"
+ "}\n"
+ "a:link, a:visited {\n"
+ " color: #38488f;\n"
+ " text-decoration: none;\n"
+ "}\n"
+ "@media (max-width: 700px) {\n"
+ " body {\n"
+ " background-color: #fff;\n"
+ " }\n"
+ " div {\n"
+ " width: auto;\n"
+ " margin: 0 auto;\n"
+ " border-radius: 0;\n"
+ " padding: 1em;\n"
+ " }\n"
+ "}\n"
+ "</style> \n"
+ "</head>\n\n"
+ "<body>\n"
+ "<div>";
final String subst = "$1$2";
final Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
final Matcher matcher = pattern.matcher(string);
// The substituted value will be contained in the result variable
final String result = matcher.replaceAll(subst);
System.out.println("Substitution result: " + result);
}
}
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