2010-06-14 67 views
0

我正在尋找一些正則表達式來幫助解析我的CSV文件。CSV與數字的正則表達式

該文件具有

number,number 
number,number 
Comment I want to skip 
number,number 
number,number 

防爆行:

319,5446 
564425,87 
Text to skip 
27,765564 

我讀每一行成一個字符串,我想用一些正規的快遞,以確保該行的模式匹配(號,數量)。如果沒有,那麼不要使用該行。

回答

3

這裏是一個解決方案:

^\ d + \ d + $

+0

謝謝...讓我給它一個鏡頭。 – 2010-06-14 20:54:37

+0

太棒了,效果很棒!謝謝。 – 2010-06-14 20:58:15

1

這應該與你的號碼和線路開始/結束:

^\d+,\d+$ 
0

哪種語言做你想要做的這在? 在Perl中:

read in each line of the csv file and save to $line 
if($line !~ m/\d+, \d+/) { 
    Do whatever you are going to do with the line with 2 numbers 
} 

在Java中使用this網站的幫助:

read in each line of the csv file and save to variable line 
Pattern p = Pattern.compile("\d+, \d+"); // remove space if it isn't in file 
Matcher m = p.matcher(line); 
if (m.find()) { 
    //it matches 
} 
+0

我在Java中這樣做。 – 2010-06-14 21:03:51

+0

@Bernie Perez我加入到上面的答案來專門處理Java。 – Kyra 2010-06-14 21:08:04

+0

由於逗號之後的空格,您的示例不匹配...... OP示例在逗號周圍沒有空格。 – dawg 2010-06-14 22:04:51