CSV Parsing

前端 未结 13 2124
攒了一身酷
攒了一身酷 2020-12-17 04:45

I am trying to use C# to parse CSV. I used regular expressions to find \",\" and read string if my header counts were equal to my match count.

Now this

13条回答
  •  伪装坚强ぢ
    2020-12-17 05:34

    If all your values are guaranteed to be in quotes, look for values, not for commas:

    ("".*?""|"[^"]*")
    

    This takes advantage of the fact that "the earliest longest match wins" - it looks for double quoted values first, and with a lower priority for normal quoted values.

    If you don't want the enclosing quote to be part of the match, use:

    "(".*?"|[^"]*)"
    

    and go for the value in match group 1.

    As I said: Prerequisite for this to work is well-formed input with guaranteed quotes or double quotes around each value. Empty values must be quoted as well! A nice side-effect is that it does not care for the separator char. Commas, TABs, semi-colons, spaces, you name it. All will work.

提交回复
热议问题