转义字符串字符

时间:2015-09-03 11:05:44

标签: ruby string

我有一个字符串,如下所示:

"\"funlolx.com, Likesgag.com (269556)\",3,0,0.000000000000000000"

我想要下面的字符串,以便我可以按","

拆分它
"funlolx.com Likesgag.com (269556),3,0,0.000000000000000000"

所以我得到的最终输出将是:

["funlolx.com Likesgag.com (269556)", "3", "0", "0.000000000000000000"]

2 个答案:

答案 0 :(得分:2)

"\"funlolx.com, Likesgag.com (269556)\",3,0,0.000000000000000000"
.gsub(/"[^"]+"/){|s| s.delete('",')}
# => "funlolx.com Likesgag.com (269556),3,0,0.000000000000000000"

答案 1 :(得分:1)

对我来说这看起来很像CSV:

require 'csv'
table = CSV.parse('"funlolx.com, Likesgag.com (269556)",3,0,0.000000000000000000')
# => [["funlolx.com, Likesgag.com (269556)", "3", "0", "0.000000000000000000"]]

注意:CSV期望获得一个表,即一个数组数组。在这种情况下,表中只有一行,所以你想获得第一行:

table.first
# => ["funlolx.com, Likesgag.com (269556)", "3", "0", "0.000000000000000000"]

如果确实必须删除逗号,那么您可以使用Ruby的所有标准字符串操作:

table.first.map {|s| s.delete(',') }
# => ["funlolx.com Likesgag.com (269556)", "3", "0", "0.000000000000000000"]