...

View Full Version : regex for CSV file



chump2877
12-20-2006, 01:11 AM
I need a "smart" regex that will recognize all possible "formatting variations" of a CSV file line, and split the file line into the appropriate pieces...For example, some common variations of a CSV file's formatting (for a single line) include:


"data1";"data2";"data3";"data4"


"data1","data2","data3","data4"


data1,data2,data3,data4


data1;data2;data3;data4

In my CSV files, double quotes will be escaped if they appear inside data fields enclosed by double quotes. For example,


"This is \"my\" data";"This is data; This is also data";"More Data"

The regex that I need needs to differentiate between escaped double quotes and regular double quotes...and it needs to accommodate for the CSV file formatting variations shown above....

I've done some of my own fiddling with this, and so far have just come away frustrated. Any ideas are welcome (including partial-regex or non-regex solutions).

Thanks.

marek_mar
12-20-2006, 07:15 AM
(?:"(?:\\"|[^"])*"|[^;]*) - version for ";" as delimiter. Try fgetcsv() (http://www.php.net/manual/en/function.fgetcsv.php) first.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum