import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "\\(+(\\H\\o|[P]|\\P\\o|[G])\\w+\\s(\\(\\w+\\s|\\(\\w+\\s\\()(\\(\\w+\\s\\w+\\s\\)\\d+\\s\\)\\d+\\s\\)\\d+\\s\\)\\d+\\s\\)|\\w+\\s\\)\\d+\\s\\)\\d+\\s\\w+\\s+\\)\\d+\\s\\)\\d+\\s)";
final String string = "(Macaca_mulatta Colobus_angolensis_palliatus (Pan_troglodytes_troglodytes ((Hylobates_agilis (Nomascus_leucogenys Symphalangus_syndactylus )21 )87 (Pongo_abelii (Homo_sapiens Gorilla_gorilla )64 )20 )12 )94 )\n"
+ "\\)\\d+\\s\\)\\d+\\s\n"
+ "(\\(\\w+\\s|\\w+\\s)(\\)\\d+|\\w+)\\s(\\w+\\s\\)\\d+|\\)\\d+\\s\\)\\d+)\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Nomascus_leucogenys (Symphalangus_syndactylus Hylobates_agilis )55 )100 (Pongo_abelii (Homo_sapiens (Pan_troglodytes_troglodytes Gorilla_gorilla )63 )100 )83 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Nomascus_leucogenys (Symphalangus_syndactylus Hylobates_agilis )86 )100 (Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )99 )100 )73 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Symphalangus_syndactylus (Hylobates_agilis Nomascus_leucogenys )91 )100 (Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )81 )100 )87 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Symphalangus_syndactylus (Hylobates_agilis Nomascus_leucogenys )72 )100 (Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )94 )100 )93 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Symphalangus_syndactylus (Hylobates_agilis Nomascus_leucogenys )58 )100 (Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )90 )100 )83 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Nomascus_leucogenys (Hylobates_agilis Symphalangus_syndactylus )68 )100 (Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )81 )100 )72 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Symphalangus_syndactylus (Hylobates_agilis Nomascus_leucogenys )90 )100 (Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )92 )100 )99 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Nomascus_leucogenys (Hylobates_agilis Symphalangus_syndactylus )68 )100 (Pongo_abelii (Homo_sapiens (Pan_troglodytes_troglodytes Gorilla_gorilla )89 )100 )99 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Symphalangus_syndactylus (Hylobates_agilis Nomascus_leucogenys )47 )100 (Pongo_abelii (Pan_troglodytes_troglodytes (Gorilla_gorilla Homo_sapiens )48 )100 )100 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Nomascus_leucogenys (Hylobates_agilis Symphalangus_syndactylus )81 )100 (Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )71 )100 )100 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )29 )67 )84 (Hylobates_agilis (Symphalangus_syndactylus Nomascus_leucogenys )36 )99 )98 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Nomascus_leucogenys (Symphalangus_syndactylus Hylobates_agilis )58 )100 (Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )82 )100 )93 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Nomascus_leucogenys (Hylobates_agilis Symphalangus_syndactylus )68 )100 (Pongo_abelii (Homo_sapiens (Gorilla_gorilla Pan_troglodytes_troglodytes )49 )100 )91 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Nomascus_leucogenys (Symphalangus_syndactylus Hylobates_agilis )58 )100 (Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )45 )100 )99 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Hylobates_agilis (Symphalangus_syndactylus Nomascus_leucogenys )35 )100 (Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )43 )100 )100 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Nomascus_leucogenys (Symphalangus_syndactylus Hylobates_agilis )45 )100 (Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )90 )100 )100 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Symphalangus_syndactylus (Hylobates_agilis Nomascus_leucogenys )44 )100 (Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )63 )100 )100 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )97 )100 )97 ((Nomascus_leucogenys Symphalangus_syndactylus )40 Hylobates_agilis )100 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Nomascus_leucogenys (Hylobates_agilis Symphalangus_syndactylus )43 )100 (Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )46 )100 )96 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Symphalangus_syndactylus (Hylobates_agilis Nomascus_leucogenys )46 )100 (Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )100 )100 )99 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Symphalangus_syndactylus (Hylobates_agilis Nomascus_leucogenys )52 )100 (Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )94 )100 )100 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Symphalangus_syndactylus (Hylobates_agilis Nomascus_leucogenys )57 )98 (Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )53 )100 )84 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Symphalangus_syndactylus (Hylobates_agilis Nomascus_leucogenys )55 )100 (Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )98 )100 )100 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Symphalangus_syndactylus (Hylobates_agilis Nomascus_leucogenys )75 )100 (Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )85 )100 )100 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Nomascus_leucogenys (Symphalangus_syndactylus Hylobates_agilis )84 )100 (Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )98 )100 )93 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )58 )100 )99 (Symphalangus_syndactylus (Hylobates_agilis Nomascus_leucogenys )53 )100 )100 )\n\n"
+ "\\(\\((\\H\\y|[NS])\\w+\\s(\\(\\w+\\s|\\w+\\s)(\\)\\d+|\\w+)\\s(\\w+\\s\\)\\d+|\\)\\d+\\s\\)\\d+)\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Nomascus_leucogenys (Symphalangus_syndactylus Hylobates_agilis )57 )100 (Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )93 )100 )100 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Hylobates_agilis (Symphalangus_syndactylus Nomascus_leucogenys )39 )100 (Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )95 )100 )100 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Nomascus_leucogenys (Hylobates_agilis Symphalangus_syndactylus )83 )100 (Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )73 )100 )99 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )85 )100 )99 ((Hylobates_agilis Symphalangus_syndactylus )68 Nomascus_leucogenys )100 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Nomascus_leucogenys (Symphalangus_syndactylus Hylobates_agilis )54 )100 (Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )79 )100 )100 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Symphalangus_syndactylus (Hylobates_agilis Nomascus_leucogenys )37 )100 (Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )55 )100 )99 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Pongo_abelii (Gorilla_gorilla (Homo_sapiens Pan_troglodytes_troglodytes )86 )100 )78 (Symphalangus_syndactylus (Hylobates_agilis Nomascus_leucogenys )64 )98 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Nomascus_leucogenys (Symphalangus_syndactylus Hylobates_agilis )55 )100 (Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )92 )100 )98 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )99 )100 )95 ((Hylobates_agilis Symphalangus_syndactylus )62 Nomascus_leucogenys )100 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Nomascus_leucogenys (Hylobates_agilis Symphalangus_syndactylus )76 )100 (Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )92 )100 )99 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Symphalangus_syndactylus (Hylobates_agilis Nomascus_leucogenys )43 )100 (Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )63 )100 )99 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Nomascus_leucogenys (Hylobates_agilis Symphalangus_syndactylus )75 )100 (Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )94 )100 )100 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Symphalangus_syndactylus (Nomascus_leucogenys Hylobates_agilis )31 )100 (Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )84 )100 )100 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Nomascus_leucogenys (Hylobates_agilis Symphalangus_syndactylus )60 )100 (Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )92 )100 )95 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )88 )100 )100 ((Hylobates_agilis Symphalangus_syndactylus )52 Nomascus_leucogenys )100 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )99 )100 )99 ((Hylobates_agilis Symphalangus_syndactylus )43 Nomascus_leucogenys )100 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Nomascus_leucogenys (Hylobates_agilis Symphalangus_syndactylus )62 )100 (Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )91 )100 )98 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Nomascus_leucogenys (Hylobates_agilis Symphalangus_syndactylus )54 )98 (Pongo_abelii (Homo_sapiens (Gorilla_gorilla Pan_troglodytes_troglodytes )48 )100 )84 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Nomascus_leucogenys (Symphalangus_syndactylus Hylobates_agilis )64 )100 (Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )100 )100 )99 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Symphalangus_syndactylus (Hylobates_agilis Nomascus_leucogenys )65 )100 (Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )76 )100 )99 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Nomascus_leucogenys (Symphalangus_syndactylus Hylobates_agilis )65 )100 (Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )84 )100 )100 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )98 )100 )94 ((Hylobates_agilis Symphalangus_syndactylus )40 Nomascus_leucogenys )100 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Symphalangus_syndactylus (Hylobates_agilis Nomascus_leucogenys )53 )100 (Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )98 )100 )100 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Nomascus_leucogenys (Symphalangus_syndactylus Hylobates_agilis )58 )100 (Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )89 )100 )100 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Nomascus_leucogenys (Hylobates_agilis Symphalangus_syndactylus )77 )100 (Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )91 )100 )100 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Nomascus_leucogenys (Hylobates_agilis Symphalangus_syndactylus )52 )100 (Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )89 )100 )100 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Nomascus_leucogenys (Hylobates_agilis Symphalangus_syndactylus )71 )100 (Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )96 )100 )100 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Nomascus_leucogenys (Symphalangus_syndactylus Hylobates_agilis )59 )100 (Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )92 )100 )100 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Nomascus_leucogenys (Hylobates_agilis Symphalangus_syndactylus )67 )100 (Pongo_abelii (Homo_sapiens (Pan_troglodytes_troglodytes Gorilla_gorilla )83 )100 )82 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Nomascus_leucogenys (Symphalangus_syndactylus Hylobates_agilis )70 )100 (Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )99 )100 )100 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Nomascus_leucogenys (Hylobates_agilis Symphalangus_syndactylus )54 )100 (Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )100 )100 )100 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Nomascus_leucogenys (Symphalangus_syndactylus Hylobates_agilis )63 )100 (Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )92 )100 )100 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Nomascus_leucogenys (Hylobates_agilis Symphalangus_syndactylus )57 )100 (Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )99 )100 )100 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Nomascus_leucogenys (Hylobates_agilis Symphalangus_syndactylus )62 )100 (Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )99 )100 )100 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Nomascus_leucogenys (Hylobates_agilis Symphalangus_syndactylus )56 )100 (Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )100 )100 )100 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Nomascus_leucogenys (Symphalangus_syndactylus Hylobates_agilis )63 )100 (Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )99 )100 )100 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Nomascus_leucogenys (Symphalangus_syndactylus Hylobates_agilis )57 )100 (Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )93 )100 )100 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )95 )100 )100 ((Hylobates_agilis Symphalangus_syndactylus )39 Nomascus_leucogenys )100 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Nomascus_leucogenys (Hylobates_agilis Symphalangus_syndactylus )68 )100 (Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )97 )100 )100 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Nomascus_leucogenys (Hylobates_agilis Symphalangus_syndactylus )49 )100 ((Pan_troglodytes_troglodytes (Homo_sapiens Gorilla_gorilla )48 )100 Pongo_abelii )61 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Nomascus_leucogenys (Symphalangus_syndactylus Hylobates_agilis )63 )100 (Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )96 )100 )100 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Symphalangus_syndactylus (Hylobates_agilis Nomascus_leucogenys )97 )100 (Pongo_abelii (Gorilla_gorilla (Homo_sapiens Pan_troglodytes_troglodytes )73 )100 )97 )100 )\n\n"
+ "(Macaca_mulatta Colobus_angolensis_palliatus ((Nomascus_leucogenys (Hylobates_agilis Symphalangus_syndactylus )81 )100 (Pongo_abelii (Gorilla_gorilla (Pan_troglodytes_troglodytes Homo_sapiens )54 )100 )72 )100 )\n\n";
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