如何删除多余的双引号?
问题描述:
在格式错误的 .csv 文件中,有一行数据带有额外的双引号,例如最后一行:
In a malformed .csv file, there is a row of data with extra double quotes, e.g. the last line:
Name,Comment
"Peter","Nice singer"
"Paul","Love "folk" songs"
如何去掉folk
周围的双引号并将字符串替换为:
How can I remove the double quotes around folk
and replace the string as:
Name,Comment
"Peter","Nice singer"
"Paul","Love _folk_ songs"
答
在 Ruby 1.9 中,以下工作:
In Ruby 1.9, the following works:
result = subject.gsub(/(?<!^|,)"(?!,|$)/, '_')
以前的版本没有后视断言.
Previous versions don't have lookbehind assertions.
说明:
(?<!^|,) # Assert that we're not at the start of the line or right after a comma
" # Match a quote
(?!,|$) # Assert that we're not at the end of the line or right before a comma
当然这是假设我们不会遇到像
Of course this assumes that we won't run into pathological cases like
"Mary",""Oh," she said"