我下面對紅寶石的正則表達式的教程工作,但似乎不工作方法match1.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 >
讓我們來完成解決問題的步驟。錯誤消息說類'match1.class => MatchData'沒有實例方法'regex'。該類列表實例方法的* [docs](http://www.ruby-doc.org/core-2.1.3/MatchData.html)*('#==', '#[]',' #begin','#captures'等等)。沒有'regex'方法,但有一個'regexp'方法,所以你似乎拼錯了方法名。另外:在你的答案中包含'str1'和'str2'會有幫助,因此讀者可以重現你的結果。 – 2014-11-03 17:17:32