方法match1.regex似乎不适用于ruby

时间:2014-11-03 15:33:20

标签: ruby-on-rails ruby ruby-on-rails-4 ruby-on-rails-3.1

我正在关注ruby中的正则表达式教程,但是一个名为regex的方法似乎无法正常工作

Tutorial

re = /(\w*)\s(\w*),\s?([\w\s]*)/
match1 = str1.match re
match2 = str2.match re
match1.regex # => wsw,s[ws]     (this is IRB's unique way of showing regular expressions; it will still work normally)

My console

The regex method the method throws an error

1.9.3-p547 :033 > re = /(\w*)\s(\w*),\s?([\w\s]*)/
 => /(\w*)\s(\w*),\s?([\w\s]*)/ 
1.9.3-p547 :034 >     match1 = str1.match re
 => #<MatchData "Joe Schmo, Plumber" 1:"Joe" 2:"Schmo" 3:"Plumber"> 
1.9.3-p547 :035 >     match2 = str2.match re
 => #<MatchData "Stephen Harper, Prime Minister" 1:"Stephen" 2:"Harper" 3:"Prime Minister"> 
1.9.3-p547 :036 > match1.regex
NoMethodError: undefined method `regex' for #<MatchData "Joe Schmo, Plumber" 1:"Joe" 2:"Schmo" 3:"Plumber">
    from (irb):36
    from /home/fernando/.rvm/rubies/ruby-1.9.3-p547/bin/irb:12:in `<main>'
1.9.3-p547 :037 > 

1 个答案:

答案 0 :(得分:0)

我认为应该是

match1.regexp

最后的'p'