2012-09-16 31 views
0

我試圖創建一個程序,它從目錄中讀取CSV文件,使用正則表達式解析文件的每一行,並在匹配正則表達式模式後顯示行。 例如,如果這是我的csv文件的第一行在Java中使用正則表達式解析CSV文件

1997,Ford,E350,"ac, abs, moon",3000.00 

我的輸出應該是

1997 Ford E350 ac, abs, moon 3000.00 

我不希望使用任何現有的CSV庫。我不擅長正則表達式,我用了一個我在網上找到的正則表達式,但它在我的程序中不起作用 這是我的源代碼,如果有人告訴我我要修改的地方和內容,我將不勝感激爲了使我的工作。請給我解釋一下。

import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.nio.CharBuffer; 
import java.nio.MappedByteBuffer; 
import java.nio.channels.FileChannel; 
import java.nio.charset.Charset; 
import java.nio.charset.CharsetDecoder; 
import java.util.regex.Pattern; 
import java.util.regex.Matcher; 


public class RegexParser { 

private static Charset charset = Charset.forName("UTF-8"); 
private static CharsetDecoder decoder = charset.newDecoder(); 
String pattern = "\"([^\"]*)\"|(?<=,|^)([^,]*)(?=,|$)"; 

void regexparser(CharBuffer cb) 
{ 
    Pattern linePattern = Pattern.compile(".*\r?\n"); 
    Pattern csvpat = Pattern.compile(pattern); 
    Matcher lm = linePattern.matcher(cb); 
    Matcher pm = null; 

    while(lm.find()) 
    { 
     CharSequence cs = lm.group(); 
     if (pm==null) 
      pm = csvpat.matcher(cs); 
      else 
       pm.reset(cs); 
     if(pm.find()) 
        { 

      System.out.println(cs); 
         } 
     if (lm.end() == cb.limit()) 
     break; 

     } 

    } 

public static void main(String[] args) throws IOException { 
    RegexParser rp = new RegexParser(); 
    String folder = "Desktop/sample"; 
    File dir = new File(folder); 
    File[] files = dir.listFiles(); 
    for(File entry: files) 
    { 
     FileInputStream fin = new FileInputStream(entry); 
     FileChannel channel = fin.getChannel(); 
     int cs = (int) channel.size(); 
     MappedByteBuffer mbb = channel.map(FileChannel.MapMode.READ_ONLY, 0, cs); 
     CharBuffer cb = decoder.decode(mbb); 
     rp.regexparser(cb); 
     fin.close(); 

    } 




} 

    } 

這是我的輸入文件

年份,牌子,型號,說明,價格

1997年,福特E350,「交流, abs,moon「,3000.00

1999,Chevy,」Venture「」Extended Edition「」「,」「,4900.00

1999年,雪佛蘭, 「創業 」「 加長版,非常大」, 「」, 「」,5000.00

1996年,吉普,大切諾基,「割愛!

空氣,天窗,裝」,4799.00

我得到一樣的地方是在我的代碼的問題?爲什麼沒有我的正則表達式對代碼的任何影響輸出?

+5

「我不想使用任何現有的CSV庫」我建議你詳細說明這一點。爲什麼不?一般來說,Regex是這個工作的錯誤工具,爲此,有很好的CSV解析庫。 – EdC

+0

String.split(http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html)對你來說還不夠嗎? :) – m4tx

+0

@EdC通過使用正則表達式我試圖擴展到其他文件格式,如通用閱讀器,通過使用不同的正則表達式我可以解析不同的格式。順便說一句,爲什麼正則表達式是錯誤的工具,你可以解釋更多? –

回答

1

反正我已經找到了解決嘍,謝謝你們的建議和幫助。

這是我最初的代碼

if(pm.find() 
     System.out.println(cs); 

現在把它改爲

while(pm.find() 
    { 
CharSequence css = pm.group(); 
//print css 
    } 

而且我用的是不同的正則表達式。我現在正在獲得所需的輸出。

+0

嘿可以 – Arun

+1

\「([^ \」] *)\「|(?<=,| ^)([^,] *)(?=,| $) –

+0

感謝您的幫助: ) – Arun

0

與此代碼[ \t]*+"[^"\r\n]*+"[ \t]*+|[^,\r\n]*+:你可以試試這個

try { 
    Pattern regex = Pattern.compile("[ \t]*+\"[^\"\r\n]*+\"[ \t]*+|[^,\r\n]*+", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.MULTILINE); 
    Matcher matcher = regex.matcher(subjectString); 
    while (matcher.find()) { 
     // Do actions 
    } 
} catch (PatternSyntaxException ex) { 
    // Take care of errors 
} 

但是,是的,如果它不是一個非常關鍵的需求也嘗試使用一些已經工作:)

+0

我是否需要在你的模式中插入任何轉義序列,如果我使用了你的模式,我會得到錯誤的結構 –

2

使用正則表達式似乎「看中」,但與CSV文件(至少在我看來)是不值得的。對於我的解析,我使用http://commons.apache.org/csv/。它從來沒有讓我失望。 :)

+0

我明白,重新發明輪子不是一個好主意,但至少爲了學習的目的,我需要一個解決方案我的程序,這就是爲什麼我在這裏提出我的問題。:) –

0

請提供建議,不要使用正則表達式來解析CSV文件。這種格式的使用方式看起來很複雜。

以下的答案必須包含鏈接到維基百科和描述CSV文件格式RFC:

+0

這一切都很好,但我只想知道爲什麼不是我的代碼工作,我無法破解我的代碼中的錯誤部分 –