import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "\\w+\\([^()]*(((?'parenthesesPair'\\()[^()]*)+((?'-parenthesesPair'\\))[^()]*)+)*(?(parenthesesPair)(?!))\\)";
final String string = "(PROT(MONITOR)TYPE(LCD)MODEL(MSTAR)CMDS(01 02 03 07 0C E3 F3)VCP(02 04 05 08 0B 0C 10 12 14(01(?(((i)))) 04 05 06 07 08 0A 0B) 16 18 1A 52 54(00 01) 60(00 11 12 0F 10 21 22 2F 30 31) 62 6C 6E 70 72(05 78 FB 50 64 78 8C A0) 86(01 02 08) 8D(01 02) A4 A5 AA(01 02) AC AE B6 C0 C6 C8 C9 CA(01 02) CC(02 03 04 05 07 08 09 0A 0C 0D 01 06 0B 0E 12 14 16 17 1A 1E 24) D6(01 04 05) DC(0E 00 01 02 03 05 08 0B 1F E1) DF E0(00 01 02 03 04) E9(00 02) EB(00 01 02 03) EC(00 01() 02 03) F0(00 01) F2(00 01 02 03 04) F6(01 ) F7(42 FF) F9(00 01 02 03 04) FF)MSWHQL(1)ASSET_EEP(40)MCCS_VER(2.2))";
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