import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?<=<!-- Template Nav Start -->[\\n])([\\n\\s\\S]*?)(?=[\\s\\n]*?<!-- Template Nav End -->)";
final String string = "<!DOCTYPE html>\n"
+ "<html lang=\"en\">\n\n"
+ " <head>\n"
+ " <meta charset=\"UTF-8\">\n"
+ " <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">\n"
+ " <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n"
+ " <meta property=\"og:site_name\" content=\"WD Proposal\">\n"
+ " <meta property=\"og:title\" content=\"Your new outdoor living space.\">\n"
+ " <meta property=\"og:image\" content=\"https://www.WhitmerDecks.com/Proposal/eProposal/img/wdLogoName.png\">\n"
+ " <meta property=\"og:description\" content=\"Here you can view the proposal from Whitmer Decks, LLC\">\n"
+ " </meta>\n\n"
+ " <!-- General scripts & styles -->\n"
+ " <link rel=\"stylesheet\" href=\"css/styles.css\">\n"
+ " <link rel=\"stylesheet\" href=\"css/cwBootstrap.css\">\n"
+ " <script src=\"js/cwBootstrap.js\" defer></script>\n\n"
+ " <!-- Google Fonts -->\n"
+ " <link href=\"https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap\" rel=\"stylesheet\">\n\n"
+ " <!-- Page title and end of general head -->\n"
+ " <title>WD Proposal</title>\n\n"
+ " <!-- Page specific scripts and styles -->\n"
+ " <script src=\"js/about.js\" defer></script>\n"
+ " </head>\n\n"
+ " <body>\n\n"
+ " <!-- Template Nav Start -->\n"
+ " <nav>\n"
+ " <ul>\n"
+ " <li><a href=\"index.html\">Home</a></li>\n"
+ " <li><a href=\"about.html\">About</a></li>\n"
+ " <li>Item X</li>\n"
+ " <li>Item Y</li>\n"
+ " </ul>\n"
+ " </nav>\n"
+ " <!-- Template Nav End -->\n\n"
+ " <p>\n"
+ " The about page\n"
+ " </p>\n\n"
+ " </body>\n\n"
+ "</html>";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
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