regex to split line (csv file)

后端 未结 7 1821
予麋鹿
予麋鹿 2020-12-03 12:37

I am not good in regex. Can some one help me out to write regex for me?

I may have values like this while reading csv file.

\"Artist,Name\",Album,12-SCS
\         


        
7条回答
  •  伪装坚强ぢ
    2020-12-03 13:17

    Just adding the solution I worked on this morning.

    var regex = new Regex("(?<=^|,)(\"(?:[^\"]|\"\")*\"|[^,]*)");
    
    foreach (Match m in regex.Matches("<-- input line -->"))
    {
        var s = m.Value; 
    }
    

    As you can see, you need to call regex.Matches() per line. It will then return a MatchCollection with the same number of items you have as columns. The Value property of each match is, obviously, the parsed value.

    This is still a work in progress, but it happily parses CSV strings like:

    2,3.03,"Hello, my name is ""Joshua""",A,B,C,,,D
    

提交回复
热议问题