tags:

views:

51

answers:

3

I'm looking for some regular expression to help parse my CSV file.

The file has lines of

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

Ex:

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

I read each line into a string and I wanted to use some regular express to make sure the line matches the pattern of (number,number). If not then don't use the line.

+3  A: 

Here is one solution:

^\d+,\d+$

mbeckish
Thanks... let me give it a shot.
Bernie Perez
Awesome, worked great! Thanks.
Bernie Perez
+1  A: 

This should match your numbers and line start/end:

^\d+,\d+$
Mikael Svenson
A: 

Which language do you want to do this in? In 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
} 

In Java use this site to help:

 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
 }
Kyra
I'm doing this in Java.
Bernie Perez
@Bernie Perez I added to the above answer to handle Java specifically.
Kyra
Your examples will not match because of the whitespace after the comma... The OP example had no whitespace around the comma.
drewk
then remove the space, as I commented at the end of the line. So instead have "\d+,\d+" The \d means it matches to a number, the + means it matches it 1 or more times (so it matches to 3 or 333333). Thus this line mean it matches one or more numbers then a comma and then one or more numbers again. The line in the code above matches if there are numbers then a comma followed by a space and then more numbers. The below website, also in link above, describes this in more detail in regards to Java.http://java.sun.com/developer/technicalArticles/releases/1.4regex/
Kyra