2011-07-27 12 views
1

ある関数から別の関数に文字列を渡して、正規表現として解析しようとしています。現在YAMLの内容は文字列と異なるのですか?

、私は

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

答えを行い、私は(正規表現の個々のセクションをマッチング)欲しいものです。

私は何が起こっている

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

用コンソールでまったく同じこと

ruleはYAML

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

putsから返され

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

戻りますか?私にとっては完璧

+0

文字列からRegexpを構築することができます。http://www.ruby-doc.org/core/classes/Regexp.html#M001228 – rubish

+1

ちょうど脇に、 '| hatは天気ですfoo? '?それはそうだから。おそらく '[wW]'だけがあなたのキャラクタークラスとして欲しいでしょう。 –

+0

質問の概要:表示されていないコードは動作しません。どうしたの? –

答えて

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"> 

何か他のものは、あなたが私たちを示していないことが起こっています。

+0

あなたは正しいです。私はその内容と@matchを混同していました。それはずっと働いていた。 – Rio

関連する問題