Java: splitting a comma-separated string but ignoring commas in parentheses

后端 未结 4 550
無奈伤痛
無奈伤痛 2020-12-06 15:45

I have a string like this:

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

the above string should split into:

         


        
4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-06 16:29

    Use Regex.

        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());
    

提交回复
热议问题