Ruby 使用正则表达式删除字符串中的\项

Ruby 使用正则表达式删除字符串中的\项,ruby,regex,Ruby,Regex,我有一个如下所示的示例字符串 "{:@text=>"\"Question 12\" must be answered when \"question 10 / Question 11 (or Question 9)\" answered"}" 我只想提取文本: “在回答问题10/问题11(或问题9)时,必须回答问题12” 我试着用这个: @object.scan(/@text=>"(.*?)"/).flatten.map{ |msg| msg.gsub(/(\.|\s+)/,

我有一个如下所示的示例字符串

 "{:@text=>"\"Question 12\" must be answered when  \"question 10 / Question 11 (or Question 9)\" answered"}"
我只想提取文本:

“在回答问题10/问题11(或问题9)时,必须回答问题12”

我试着用这个:

@object.scan(/@text=>"(.*?)"/).flatten.map{ |msg| msg.gsub(/(\.|\s+)/, '').strip }
但是,它只返回一个
\


我还可以尝试什么?

您可以尝试使用
/\/\w+/

[13] pry(main)> str
=> "{:@text=>\"\\\"Question 12\\\" must be answered when  \\\"question 10 / Question 11 (or Question 9)\\\" answered\"}"
[14] pry(main)> str.scan(/\/|\w+/).join(" ")
=> "text Question 12 must be answered when question 10 / Question 11 or Question 9 answered"

这不是一个有效的字符串。您拥有以下字符串:

 "#{:@text=>"
其次是:

 \"Question 12\" must be answered when  \"question 10 / Question 11 (or Question 9)\" answered"}"
使用单引号:

str = '{:@text=>"\"Question 12\" must be answered when  \"question 10 / Question 11 (or Question 9)\" answered"}'
然后

puts str.gsub(/\{:@text=>|\"|\\|\}/,'')
  #=> Question 12 must be answered when  question 10 / Question 11 (or Question 9) answered