How to parse CSV data with Regular Expressions.
September 2010.
/((?:,|^)(?!,)(?:"(.*?(?
Here is an example in PHP:
$data = 'zerzer,"abcd","sd(fsdf\"dsfsfsd","sdf,e)zr,ert\nfdg",,,,ssfsdfsdf,"sdfsdfsdf",sfsdfs';
$regexp = '¤((?:,|^)(?!,)(?:"(.*?(?$count = preg_match_all($regexp, $data, $m);
$row = array();
if ($count) {
for($i=0, $acount=count($m[0]); $i<$acount; $i++) {
$row[] = stripslashes($m[2][$i].$m[3][$i].$m[4][$i]);
}
}
print_r($row);
Output:
Array
(
[0] => zerzer
[1] => abcd
[2] => sd(fsdf"dsfsfsd
[3] => sdf,e)zr,ertnfdg
[4] =>
[5] =>
[6] =>
[7] => ssfsdfsdf
[8] => sdfsdfsdf
[9] => sfsdfs
)