Regular expression to find and replace unescaped Non-successive double quotes in CSV file

后端 未结 3 817
日久生厌
日久生厌 2021-01-06 16:35

This is an extension to a related question answered Here

I have a weekly csv file which needs to be parsed. it looks like this.

\"asdf\",\"asdf\",\"as

3条回答
  •  太阳男子
    2021-01-06 16:52

    In vim I used this to remove all the unescaped quotes.

    :%s/\v("(,")@!)&((",)@

    detailed explanation is,

    : - start the vim command
        % - scope of the command is the whole file
        s - search and replace
            / - start of search pattern
            \v - simple regex syntax (rather than vim style)
                (
                    " - double quote
                    (,") - comma_quote
                    @! - not followed by
                )
                & - and
                (
                    (",) - quote_comma
                    @

    this does the job fairly quickly. The only instance this fails is when there are instances of "," pattern in the data.

提交回复
热议问题