在格式错误的.csv文件中,有一行带有额外双引号的数据,例如最后一行:
Name,Comment
"Peter","Nice singer"
"Paul","Love "folk" songs"
Run Code Online (Sandbox Code Playgroud)
如何删除双引号folk并将字符串替换为:
Name,Comment
"Peter","Nice singer"
"Paul","Love _folk_ songs"
Run Code Online (Sandbox Code Playgroud)
在Ruby 1.9中,以下工作:
result = subject.gsub(/(?<!^|,)"(?!,|$)/, '_')
Run Code Online (Sandbox Code Playgroud)
以前的版本没有lookbehind断言.
说明:
(?<!^|,) # 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
Run Code Online (Sandbox Code Playgroud)
当然,这假设我们不会遇到类似病态的病例
"Mary",""Oh," she said"
Run Code Online (Sandbox Code Playgroud)