import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "\\<(ol)(?: .*?)?\\>(?:[^\\<]|\\<(.*?)\\>.*\\<\\/\\2\\>)*\\<\\/\\1\\>";
final String string = "<ol>kjkjkj\n\n"
+ "</ol>\n\n\n\n"
+ "<ol s>\n"
+ " <li>Table of Contents.....................CT.TS</li>\n"
+ " <li>Gamecube Controls.....................CN.GC</li>\n"
+ " <li>Nintendo 64 Controls..................CN.64</li>\n"
+ " <li>Walkthrough...........................01.00\n"
+ " <ol>\n"
+ " <li>Kokiri Forest...............01.01</li>\n"
+ " </ol>\n"
+ " </li> \n"
+ " <li>Sidequests & Minigames................02.00\n"
+ " <ol>\n"
+ " <li>Shooting Gallery............02.01</li>\n"
+ " </ol>\n"
+ " </li>\n"
+ " <li>Boss Guide............................03.00\n"
+ " <ol>\n"
+ " <li>Gohma.......................03.01</li>\n"
+ " <li>Final Boss [Second Form]....03.15</li>\n"
+ " </ol>\n"
+ " </li>\n"
+ " <li>Item Checklist........................04.00\n"
+ " <ol>\n"
+ " <li>Final Dungeon...............04.47</li>\n"
+ " </ol>\n"
+ " </li>\n"
+ " <li>Shop Inventory........................05.00 \n"
+ " <ol>\n"
+ " <li>Kokiri Shop [Young].........05.01</li>\n"
+ " <li>Goron Shop [Adult]..........05.09</li>\n"
+ " </ol>\n"
+ " </li>\n"
+ " <li>Ocarina Notes.........................06.00\n"
+ " <ol><ol>sdsds</ol>\n"
+ " <li>Requ<ol></ol>iem of Spirit...........06.12</li>\n"
+ " </ol>\n"
+ " </li>\n"
+ " <li>Heart Containers......................07.00</li>\n"
+ " <li>Item List.............................15.00</li>\n"
+ " <li>Legal & Copyright.....................LE.AL</li>\n"
+ " <li>Credits & Thanks......................CR.DS</li>\n"
+ " </ol>\n\n"
+ "<ol><ol><ol><ol><ol></ol></ol></ol></ol></ol>\n\n\n"
+ "sdsd\n"
+ "k<ol>jlkjlkj \n"
+ "kjbkjh</ol> ";
final Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
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