我是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_。 –