初步实现仅供参考
@Test
void main(){
test(0,"(x(()())()x)()");
}
static void test(int i, String str){
//String str = "(x(()())()x)()";
if (str.isEmpty()) return;
Pattern pattern = Pattern.compile("^(\\(?[^\\(^\\)]?)(\\)?)(.*$)");
Matcher matcher = pattern.matcher(str);
while (matcher.find()){
if (matcher.group(1).contains("(")) i++;
if (matcher.group(2).contains(")")) i--;
if (i==0) return;
test(i,matcher.group(3));
}
}