Regex find comma not inside quotes

前端 未结 5 1985
不思量自难忘°
不思量自难忘° 2020-12-05 07:52

I\'m checking line by line in C#

Example data:

bob jones,123,55.6,,,\"Hello , World\",,0
jim neighbor,432,66.5,,,Andy \"Blank,,1
john smith,555,77.4,         


        
5条回答
  •  时光取名叫无心
    2020-12-05 07:53

    The below regex is for parsing each fields in a line, not an entire line

    Apply the methodical and desperate regex technique: Divide and conquer

    Case: field does not contain a quote

    • abc,
    • abc(end of line)

    [^,"]*(,|$)

    Case: field contains exactly two quotes

    • abc"abc,"abc,
    • abc"abc,"abc(end of line)

    [^,"]*"[^"]*"[^,"]*(,|$)

    Case: field contains exactly one quote

    • abc"abc(end of line)
    • abc"abc, (and that there's no quote before the end of this line)

    [^,"]*"[^,"]$

    [^,"]*"[^"],(?!.*")

    Now that we have all the cases, we then '|' everything together and enjoy the resultant monstrosity.

提交回复
热议问题