我是CSV解析的新手。我有一个CSV文件,第三列(描述字段)可能有一个或多个6位数字以及其他值。我需要过滤掉这些数字并将它们写入每行对应的相邻列。使用Java过滤掉CSV文件中的数字
如:
3rd column 4th column
============= ===========
123456adjfghviu77 123456
shgdasd234567 234567
123456abc:de234567:c567890d 123456-234567-567890
12654352474
请帮助。这是我迄今为止所做的。
String strFile="D:/Input.csv";
CSVReader reader=new CSVReader(new FileReader(strFile));
String[] nextline;
//int lineNumber=0;
String str="^[\\d|\\s]{5}$";
String regex="[^\\d]+";
FileWriter fw = new FileWriter("D:/Output.csv");
PrintWriter pw = new PrintWriter(fw);
while((nextline=reader.readNext())!=null){
//lineNumber++;
//System.out.println("Line : "+lineNumber);
if(nextline[2].toString().matches(str)){
pw.print(nextline[1]);
pw.append('\n');
System.out.println(nextline[2]);
}
}
pw.flush();
*可能有一个或一个以上6位数字与其他值一起* - >你必须向我们展示了样品的输入和输出 – TheLostMind
对不起......更新与样品输入的问题。 –
问题是,你只是检查是否正则表达式模式_matches_,然后打印该行,如果它。您需要使用捕获组并打印_submatches_。 –