2010-06-14 66 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