split string on comma, but not commas inside parenthesis

后端 未结 3 2039
没有蜡笔的小新
没有蜡笔的小新 2021-01-23 23:09

Now I have a task about Java IO , I need to read file line by line and save this data into database, But Now I got a problem.

\"desk\",\"12\",\"15\",\"(small         


        
3条回答
  •  面向向阳花
    2021-01-23 23:43

    You could solve this by using Pattern and Matcher. Any solution using split would just seem like a nasty workaround. Here's an example:

    public static void main(String[] args){
        String s = "\"desk\",\"12\",\"15\",\"(small,median,large)\"";
        Pattern p = Pattern.compile("\".+?\"");
        Matcher m = p.matcher(s);
        List matches = new ArrayList();
    
        while (m.find()){
            matches.add(m.group());
        }
    
        System.out.println(matches);
    }
    

提交回复
热议问题