Java:拆分以逗号分隔的字符串,但忽略括号中的逗号

我有一个像这样的字符串:

one,two,3,(4,five),six,(seven),(8,9,ten),eleven,(twelve,13,14,fifteen)

上面的字符串应该分成:

 one two 3 (4,five) six (seven) (8,9,ten) eleven (twelve,13,14,fifteen) 

我认为最简单的解决方案是处理输入字符串char-by-char:

 public static List split(String input) { int nParens = 0; int start = 0; List result = new ArrayList<>(); for(int i=0; i 0) throw new IllegalArgumentException("Missing closing parenthesis"); result.add(input.substring(start)); return result; } 

例:

 split("one,two,3,(4,five),six,(seven),(8,9,ten),eleven,(twelve,13,14,fifteen)") -> [one, two, 3, (4,five), six, (seven), (8,9,ten), eleven, (twelve,13,14,fifteen)] 

作为免费奖励,此解决方案还会在必要时计算嵌套括号:

 split("one,two,3,(4,(five,six),seven),eight") -> [one, two, 3, (4,(five,six),seven), eight] 

它还检查括号是否平衡(每个左括号都有相应的结束括号)。

有一个相对简单的单行解决方案:

 String[] parts = input.split(",(?![^()]*\\))"); 

干得好… :)

 import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; /** * * @author S1LENT W@RRIOR */ public class Tokenizer { public static void main(String... args) { List tokens = new ArrayList(); // List to store tokens String string = "one,two,3,(4,five),six,(seven),(8,9,ten),eleven,(twelve,13,14,fifteen)"; // input string StringTokenizer tokenizer = new StringTokenizer(string, "(),", true); // tokenize your string on the basis of these parentheses while (tokenizer.hasMoreElements()) { // iterate over tokens String aToken = (String) tokenizer.nextElement(); // get a token if (aToken.equals("(")) { // if token is the begining of a parenthisis StringBuilder sb = new StringBuilder(aToken); String nextToken = (String) tokenizer.nextElement(); // get next token while (!nextToken.equals(")")) { // iterate over next tokens untill you find the ending bracket sb.append(nextToken); nextToken = (String) tokenizer.nextElement(); } sb.append(")"); tokens.add(sb.toString()); // add to tokens list } else if(aToken.equals(",")) { // need this to avoid adding commas // do nothing } else { tokens.add(aToken); // add to tokens list } } for(String aToken: tokens) { // print all tokens System.out.println(aToken); } } } 

使用正则表达式。

  String s = "one,two,3,(4,five),six,(seven),(8,9,ten),eleven,(twelve,13,14,fifteen)"; Matcher m = Pattern.compile("[^,()]+|\\([^)]*\\)").matcher(s); while (m.find()) System.out.println(m.group());