RegEx on CSV missing data (Why)

DeltaWolf7

Well-known member
Joined
Jan 21, 2006
Messages
47
Programming Experience
Beginner
Hi,

VB.NET newbie... I am using VB.Net 2005.
I am using this expression to read from csv files, but I am finding that cells that have no data are being missed instead of a blank column being inserted.

VB.NET:
[SIZE=2][COLOR=#0000ff]
Dim[/COLOR][/SIZE][SIZE=2] re [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Regex = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Regex([/SIZE][SIZE=2][COLOR=#800000]"((?<field>[^"",\r\n]+)|""(?<field>([^""]|"""")+)"")(,|(?<rowbreak>\r\n|\n|$))"[/COLOR][/SIZE][SIZE=2])
[/SIZE]

I got the CSV reader from: http://www.hotblue.com/article0000.aspx?a=0006

Is their a way to make this code insert blank columns instead of just missing the null data?

Thank you
 
Last edited:
Here is a sample line from my csv.

VB.NET:
"27.01218.201","ACER","ACER COMMERCIAL","ACER NOTEBOOK POWERCABLE UK3PIN","3.00","0","","","0.000","2.54"

What i need to do is change my regex so that i can capture "" fields and return them as empty strings.

I found this

VB.NET:
This works well but misses empty fields (,,,) this is easily fixed as in: 
((?<field>[^\",\\r\\n]*)|\"(?<field>([^\"]|\"\")*)\")(,|(?<rowbreak>\\r\\n|\\n|$)) 

which returns blank fields as empty strings but will also return a blank for the end of line so you might want to remove the trailing $ option if you know all the records will be on one line (the normal case with many spreadsheet generated csv files). You can also get rid of the <rowbreak> item (just leave the ,) if working a line at a time. [URL="javascript:__doPostBack('_ctl0$_ctl0$_ctl0$_ctl0$_ctl0$_ctl0$Comments$_ctl0$Comments$_ctl0$EditLink','')"][/URL]
[URL="http://www.vbdotnetforums.com/"]Michael[/URL]

Here: http://dotnetjunkies.com/WebLog/chris.taylor/archive/2004/04/09/11039.aspx

But I cant get it to work for me. I am new to regex and dont understand any of it yet.

Any help please?
 
Back
Top