Read a CSV file with Regular Expressions in .Net

Here’s how you can read a CSV file using Regular Expressions in .Net:

public static DataTable GetDataTableFromCsvFile(string file)
// Where the CSV data goes
DataTable dt = new DataTable("CsvData");
// The pattern used to parse the CSV
const string csvPattern = ",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))";
Regex csvRegex = new Regex(csvPattern);
// Read all lines in the file
// (not great for large files)
string[] fileLines = File.ReadAllLines(file);
// Get the column headers
// (assumes first row has headers and that
//  each column contains string values and
//  that each column name is unique)
Dictionary&lt;int, string&gt; headers = new Dictionary&lt;int, string&gt;();
string[] headerValues = csvRegex.Split(fileLines[0]);
for (int i = 0; i &lt; headerValues.Length; i++)
headers.Add(i, headerValues[i]);
dt.Columns.Add(new DataColumn(headerValues[i], typeof (string)));
// Then add the the rest of the lines
for (int k = 1; k &lt; fileLines.Length; k++)
DataRow dr = dt.NewRow();
string line = fileLines[k];
string[] cols = csvRegex.Split(line);
for (int i = 0; i &lt; cols.Length; i++)
string header = headers[i];
string data = cols[i];
// remove quotes around the field
if (data.Length &gt; 1 &amp;&amp; data.StartsWith("\"") &amp;&amp; data.EndsWith("\""))
data = data.Remove(data.Length - 1, 1).Remove(0, 1);
dr[header] = data;
return dt;

Of course, you will need to add in error catching and handling as well.

Hope this helps!

This entry was posted in C#, Regular Expressions. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *