Need help ignoring commas within a csv file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • zdrakec

    Need help ignoring commas within a csv file

    I need to be able to do a Regex.split on a csv file ignoring the comma
    if it is inside a single quote. I have the regular expression working
    in the RegexDesigner tool, but when I use it within vb.net it adds an
    extra split on each of the commas as well.

    Example:
    Reg Ex = ,(?=([^']*'[^']*')*(?![^']*'))
    String to split = " '1A', 9, 2884, 'Sally, is late', 'nothing'

    Works fine in the designer but output in vb.net looks like
    element(0) = '1A'
    element(1) = ,
    element(2) = 9
    element(3) = ,

    etc...

    Any help will be greatly appreciated!!

    Thanks,
    zdrakec

  • shriop

    #2
    Re: Need help ignoring commas within a csv file

    Despite the common thinking, regex's do not work for csv's. Even if you
    find a regex that is somewhat close to handling what you need, it will
    be slower than even the simplest string manipulation. If you're doing
    this for production code, I would strongly recommend that you use one
    of the several production level csv parsers available on the net.

    Bruce Dunwiddie
    Read, write, and bulk insert common file formats like CSV, Excel, and XML from C# and VB.Net.


    zdrakec wrote:[color=blue]
    > I need to be able to do a Regex.split on a csv file ignoring the comma
    > if it is inside a single quote. I have the regular expression working
    > in the RegexDesigner tool, but when I use it within vb.net it adds an
    > extra split on each of the commas as well.
    >
    > Example:
    > Reg Ex = ,(?=([^']*'[^']*')*(?![^']*'))
    > String to split = " '1A', 9, 2884, 'Sally, is late', 'nothing'
    >
    > Works fine in the designer but output in vb.net looks like
    > element(0) = '1A'
    > element(1) = ,
    > element(2) = 9
    > element(3) = ,
    >
    > etc...
    >
    > Any help will be greatly appreciated!!
    >
    > Thanks,
    > zdrakec[/color]

    Comment

    • zdrakec

      #3
      Re: Need help ignoring commas within a csv file

      Thank you sir!

      Cheers,
      zdrakec

      Comment

      Working...