Search code examples
csvhelper

CSVHelper remove space and dashes from headers


I have a project where the CSV file header has both spaces and dashes in the column header names.

The headers look like this:

First Name, Last Name, E-mail Address, Phone

I am successful in removing the spaces using the following code:

PrepareHeaderForMatch = header => Regex.Replace(header.Header, @"\s", string.Empty)

I have been unsuccessful in adding the "-" to the regex to strip out both spaces and dashes.

Is this possible?


Solution

  • Thanks!

    I was able to resolve the issue with the following regex:

    PrepareHeaderForMatch = header => Regex.Replace(header.Header, @"[\s-]+", string.Empty).ToLower()
    

    I had tried variations of "[\s-]+" and kept running into issues. This variation provided the result I needed.