【问题标题】:YAML contents different from string?YAML 内容与字符串不同?
【发布时间】:2011-10-14 10:37:42
【问题描述】:

我正在尝试将字符串从一个函数传递到另一个函数以作为正则表达式进行解析。

目前,当我这样做时

@match = run_rule({ "subjectline" => "What is the weather like in Mumbai?", "rule" => "[w|W]hat is the weather( like)? in ([^?]+)?"})

答案就是我想要的(匹配正则表达式的各个部分)。

当我这样做时

@match = run_rule({ "subjectline" => "What is the weather like in Mumbai?", "rule" => rule['rule']['rule']})

rule 从 YAML 返回的位置

---
rule:
  rule: "[w|W]hat is the weather( like)? in ([^?]+)?"

puts 在控制台返回完全相同的东西

puts rule['rule']['rule']
puts "[w|W]hat is the weather( like)? in ([^?]+)?"

发生了什么事?

【问题讨论】:

  • 您可以从字符串ruby-doc.org/core/classes/Regexp.html#M001228构建正则表达式
  • 顺便说一句,你打算匹配|hat is the weather in foo?吗?因为确实如此。您可能只希望 [wW] 作为您的角色类。
  • 问题概要:一些未显示的代码不起作用。怎么回事?

标签: ruby-on-rails ruby ruby-on-rails-3 yaml


【解决方案1】:

非常适合我,请参阅:

irb(main):079:0> yaml = <<EOI
irb(main):080:0" ---
irb(main):081:0" rule:
irb(main):082:0"   rule: "[w|W]hat is the weather( like)? in ([^?]+)?"
irb(main):083:0" EOI
=> "---\nrule:\n  rule: \"[w|W]hat is the weather( like)? in ([^?]+)?\"\n"
irb(main):084:0> rule = YAML.load(yaml)
=> {"rule"=>{"rule"=>"[w|W]hat is the weather( like)? in ([^?]+)?"}}
irb(main):085:0> args = { "subjectline" => "What is the weather like in Mumbai?", "rule" => rule['rule']['rule']}
=> {"subjectline"=>"What is the weather like in Mumbai?", "rule"=>"[w|W]hat is the weather( like)? in ([^?]+)?"}
irb(main):086:0> args['subjectline'].match args['rule']
=> #<MatchData "What is the weather like in Mumbai" 1:" like" 2:"Mumbai">

发生了一些你没有向我们展示的事情。

【讨论】:

  • 你是对的。原来我把@match 和它的内容搞混了。它一直在工作。
猜你喜欢
  • 1970-01-01
  • 2018-08-03
  • 2017-04-05
  • 2014-07-15
  • 1970-01-01
  • 2010-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多