尝试从csv文件构建行时忽略逗号

时间:2015-02-20 09:12:23

标签: java regex

我正在尝试从CSV文件创建数据库表,这是我的程序:

public class ReadCVS {

public static void main(String[] args) {

    ReadCVS obj = new ReadCVS();
    obj.run();

}

public void run() {

    String csvFile = "/home/Downloads/Final_DA_Celeb_List - csv_cel_event.csv";
    BufferedReader br = null;
    String line = "";
    String cvsSplitBy = ",";

    try {
        int i = 0;
        String[] row = null;
        br = new BufferedReader(new FileReader(csvFile));
        while ((line = br.readLine()) != null) {

            i++; 
            String create = "Create Table Events"; 

            if(!(line.contains("On [Month Date "))){
                row = line.split(cvsSplitBy);
            }
            if(i == 1){
                for(int x = 0; x <row.length; x++){
                    System.out.println("Column "+x+" = "+row[x]);
                    switch (x){
                    case 0:
                        create = create + ("( "+row[x]+ " integer primary key autoincrement, "); 
                        break;
                    case 1:
                        create = create + row[x] + " text, "; 
                        break;
                    case 2:
                        create = create + row[x] + " text, "; 
                        break;
                    case 3:
                        create = create + row[x] + " text, "; 
                        break;
                    case 4:
                        create = create + row[x] + " text, "; 
                        break;
                    case 6:
                        create = create + row[x] + " text, "; 
                        break;
                    case 7:
                        create = create + row[x] + " text, "; 
                        break;
                    case 8:
                        create = create + row[x] + " text ); "; 
                        break;
                    }
                }

                System.out.println(create);
            }else{
                for(int x = 0; x <row.length; x++){
                    System.out.println("Column "+x+" = "+row[x]);
                }
            }



        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    System.out.println("Done");
}

}

我的CSV中的一个示例行如下:

On [Month Date, Year] it will be [No.] [Minutes / Seconds/ Days/ Weeks / Months] since blah blah.

在我的程序中,我想跳过上面一行中的,,我的程序正在将上面的行分成两行。

到目前为止,我尝试使用if子句:

if(!(line.contains("On [Month Date "))){
                row = line.split(cvsSplitBy);
            }

但这似乎没有帮助,任何想法如何才能得到理想的结果?

1 个答案:

答案 0 :(得分:1)

您可以更改split命令以使用RegEx表达式仅拆分不在方括号内的逗号:

String cvsSplitBy = "(?![^)(]*\\([^)(]*?\\)\\)),(?![^\\[]*\\])";
row = line.split(cvsSplitBy);

这适用于您提供的示例,但明显的限制是您不要分割的逗号必须在方括号内。