NestListParser { public static List<String> parseNestList(String str) { NestListState state = NestListState.START; ArrayList<String> list = new ArrayList<>(); field.setLength(0); int quoteCount_InParentheses = 0; int leftNotMatchCount_InParentheses = 0; boolean outMostIsFunction = false; for (char c : str.toCharArray()) { switch (state) { case START: if (c == whitespace) { } else if (c == separator) { list.add(""); } else if (c == quote) { field.setLength(0); state = NestListState.QUOTE; } else if (c == leftParentheses) { field.setLength(0); outMostIsFunction = false; leftNotMatchCount_InParentheses = 1; quoteCount_InParentheses = 0; state = NestListState.IN_PARENTHESES; } else { field.setLength(0); field.append(c); state = NestListState.NO_QUOTE; } break; case NO_QUOTE: if (c == separator) { list.add(field.toString()); state = NestListState.START; } else if (c == leftParentheses) { field.append(c); outMostIsFunction = true; leftNotMatchCount_InParentheses = 1; quoteCount_InParentheses = 0; state = NestListState.IN_PARENTHESES; } else { field.append(c); } break; case QUOTE: if (c == quote) { state = NestListState.QUOTE2; } else { field.append(c); } break; case QUOTE2: if (c == separator) { list.add(field.toString()); state = NestListState.START; } else if (c == quote) { field.append(quote); state = NestListState.QUOTE; } else { field.append(c); state = NestListState.NO_QUOTE; } break; case IN_PARENTHESES: if (c == quote) { quoteCount_InParentheses++; } if (quoteCount_InParentheses % 2 == 1) { field.append(c); } else if (c == leftParentheses) { leftNotMatchCount_InParentheses++; field.append(c); } else if (c == rightParentheses) { leftNotMatchCount_InParentheses--; if (leftNotMatchCount_InParentheses > 0 || outMostIsFunction) { field.append(c); } if (leftNotMatchCount_InParentheses == 0) { state = NestListState.PARENTHESES_OK; } } else { field.append(c); } break; case PARENTHESES_OK: if (c == whitespace) { } else if (c == separator) { list.add(field.toString()); state = NestListState.START; } else { throw new RuntimeException("最外层的()之后应该是空格和逗号"); } break; } } switch (state) { case START: break; default: list.add(field.toString()); break; } list.trimToSize(); return list; }  static List<String> parseNestList(String str); static List<String> parseFunction(String str);  }
@Test public void parseNestList() { t1("a", "a"); t1("a,b", "a", "b"); t1("QingGong(10,(13300100,13300200)), RanSe(30,3)", "QingGong(10,(13300100,13300200))", "RanSe(30,3)"); t1("QingGong(10, (13300100, 13300200)) , RanSe(30,3) ", "QingGong(10, (13300100, 13300200))", "RanSe(30,3)"); }