import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "<dl>(?:[^<]+|<(?!/?dl\\b)|(?R))*+</dl>";
final String string = "<p>\n"
+ " <span style=\"visibility:hidden\" id=\"Synonyme\">\n"
+ " <span id=\"Anker:Synonyme\"></span>\n"
+ " </span>\n"
+ "</p>\n"
+ "<div style=\"margin-bottom:-0.5em; font-weight:bold;\" title=\"bedeutungsgleich gebrauchte Wörter\">Synonyme:</div>\n"
+ "<dl>\n"
+ " <dd>[1] <a href=\"/w/index.php?title=Blutsverwandte&action=edit&redlink=1\" class=\"new\" title=\"Blutsverwandte (Seite nicht vorhanden)\">Blutsverwandte</a>,\n"
+ " <a href=\"/wiki/Sippe\" title=\"Sippe\">Sippe</a>\n"
+ " <dl>\n"
+ " <dd>[1a] <a href=\"/wiki/Kernfamilie\" title=\"Kernfamilie\">Kernfamilie</a></dd>\n"
+ " <dd>[1b] <i>abwertend, salopp:</i> <a href=\"/wiki/Mischpoke\" title=\"Mischpoke\">Mischpoke</a></dd>\n"
+ " </dl>\n"
+ " </dd>\n"
+ " <dd>[2] <a href=\"/wiki/Abart\" title=\"Abart\">Abart</a>,\n"
+ " <a href=\"/wiki/Art\" title=\"Art\">Art</a>,\n"
+ " <a href=\"/wiki/Bereich\" title=\"Bereich\">Bereich</a>,\n"
+ " <a href=\"/wiki/Departement\" title=\"Departement\">Departement</a>,\n"
+ " <a href=\"/wiki/Dialekt\" title=\"Dialekt\">Dialekt</a>,\n"
+ " <a href=\"/wiki/Fach\" title=\"Fach\">Fach</a>,\n"
+ " <a href=\"/wiki/Gattung\" title=\"Gattung\">Gattung</a>,\n"
+ " <a href=\"/wiki/Genus\" title=\"Genus\">Genus</a>,\n"
+ " <a href=\"/wiki/Geschlecht\" title=\"Geschlecht\">Geschlecht</a>,\n"
+ " <a href=\"/wiki/Gruppe\" title=\"Gruppe\">Gruppe</a>,\n"
+ " <a href=\"/wiki/Kategorie\" title=\"Kategorie\">Kategorie</a>,\n"
+ " <a href=\"/wiki/Linie\" title=\"Linie\">Linie</a>,\n"
+ " <a href=\"/wiki/Rasse\" title=\"Rasse\">Rasse</a>,\n"
+ " <a href=\"/wiki/Reihe\" title=\"Reihe\">Reihe</a>, \n"
+ " <a href=\"/wiki/Rubrik\" title=\"Rubrik\">Rubrik</a>,\n"
+ " <a href=\"/wiki/Schlag\" title=\"Schlag\">Schlag</a>\n"
+ " </dd>\n"
+ "</dl>";
final Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE | 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