Regular expression to match CSV delimiters

后端 未结 6 960
天命终不由人
天命终不由人 2020-12-17 22:39

I\'m trying to create a PCRE that will match only the commas used as delimiters in a line from a CSV file. Assuming the format of a line is this:

1,\"abcd\",         


        
6条回答
  •  囚心锁ツ
    2020-12-17 22:56

    I know this is old, but this RegEx works for me:

    /(\"[^\"]+\")|[^,]+/g
    

    It could be use potentially with any language. I tested it in JavaScript, so the g is just a global modifier. It works even with messed up lines (extra quotes), but empty is not dealt with.

    Just sharing, maybe this will help someone.

提交回复
热议问题