如何删除多余的双引号? [英] How to remove the extra double quote?

查看:83
本文介绍了如何删除多余的双引号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在格式错误的 .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"

这篇关于如何删除多余的双引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆