ruby中的脚本,用于查找“”“的所有实例,并替换为”“”“

时间:2014-03-21 19:04:27

标签: ruby regex string replace

我有这个问题,我需要用两个双引号替换文本周围的单引号。样品行如下。

“1”, “” 未知 “”, “” “” “” “”, “” 未知 “”, “” “”, “” 未知 “”, “” “”, “假”,” 1077.36“,”132“,”8.1618“,”97“,”483“,”1“,”51“,”2010-06-17 20:58:00“,”2013-12-04 12:05: 00“,”101“,”“白橡木”“,”0“,”0“,”1“,”1“

所以例如,在第一个逗号之前,我想要什么是parens。 (“”1“”)我使用的脚本看起来像这样。

file_names = ['test.csv']

file_names.each do |file_name|

  text = File.read(file_name)

File.write(file_name, text.gsub(/"/, "\"\""))

end

如果mt正则表达式看起来像这样/“{1} /它会拾取每一个双引号,所以这似乎也不起作用。

非常感谢任何帮助。谢谢。

2 个答案:

答案 0 :(得分:2)

如果我理解正确,您希望将"..."的任何实例替换为""..."",但前提是...被任意一方的双引号所包围。您的某些值已经在两边都有两个双引号,应保持不变。例如,""Unknown""应保持""Unknown"",不应成为""""Unknown""""

我假设你的'样本行'是一个单独的字符串,即它应该用引号括起来将它声明为我在我的代码中做的字符串 - 我用单引号括起来以避免大量的双重转义引号。

我使用的正则表达式使用负向lookbehind和lookahead来仅识别那些前面或后面没有双引号的双引号,并用两个双引号替换它们。

line = '"1",""Unknown"","""","""",""Unknown"","""",""Unknown"","""","False","1077.36","132","8.1618","97","483","1","51","2010-06-17 20:58:00","2013-12-04 12:05:00","101",""White Oak"","0","0","1","1"'
line.gsub(/(?<!")"(?!")/, '""')

这导致了这个(没有引号):

""1"",""Unknown"","""","""",""Unknown"","""",""Unknown"","""",""False"",""1077.36"",""132"",""8.1618"",""97"",""483"",""1"",""51"",""2010-06-17 20:58:00"",""2013-12-04 12:05:00"",""101"",""White Oak"",""0"",""0"",""1"",""1""

因此,要将此代码应用于您的代码,只需将File.write行替换为:

即可
File.write(file_name, text.gsub(/(?<!")"(?!")/, '""'))

答案 1 :(得分:1)

如果你不想在单个regexp gsub中进行替换,你可以先用不同的东西替换两个双引号(例如一个从不出现的utf-8符号),然后用2个双引号替换一个双引号最后再用2个双引号替换特殊字符。