regular expression should split , that are contained outside the double quotes in a CSV file?

前端 未结 4 797
情话喂你
情话喂你 2021-01-26 10:00

This is the sample

\"abc\",\"abcsds\",\"adbc,ds\",\"abc\"

Output should be

abc
abcsds
adbc,ds
abc
4条回答
  •  无人共我
    2021-01-26 10:32

    This answer has a C# solution for dealing with CSV.

    In particular, the line

    private static Regex rexCsvSplitter = new Regex( @",(?=(?:[^""]*""[^""]*"")*(?![^""]*""))" );
    

    contains the Regex used to split properly, i.e., taking quoting and escaping into consideration.

    Basically what it says is, match any comma that is followed by an even number of quote marks (including zero). This effectively prevents matching a comma that is part of a quoted string, since the quote character is escaped by doubling it.

    Keep in mind that the quotes in the above line are doubled for the sake of the string literal. It might be easier to think of the expression as

    ,(?=(?:[^"]*"[^"]*")*(?![^"]*"))
    

提交回复
热议问题