2014-11-03 5 views
0

에서 작동하지 않는 것 같다 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 > 
+0

문제를 해결하기위한 단계를 수행해 보겠습니다. 오류 메시지는 클래스'match1.class => MatchData'는 인스턴스 메소드'regex'를 가지고 있지 않다고 말합니다. 해당 클래스 목록 인스턴스 메소드에 대한 * [docs] (http://www.ruby-doc.org/core-2.1.3/MatchData.html) * ('# ==', '# []',' # begin', # captures 등). 'regex' 메소드는 없지만'regexp' 메소드가 있기 때문에 메소드 이름의 철자가 틀린 것으로 보입니다. 옆으로 : 당신의 대답에'str1'과'str2'를 포함시키는 것이 도움이 되었기 때문에 독자들은 결과를 재현 할 수있었습니다. –

답변

0

를 작동하지 않는 것 같다 방법이라고 정규식 나는 그것이해야한다고 생각

match1.regexp 

마지막 'p'가

+0

match1.regexp =>/(\ w *) \ s (\ w *), \ s? ([\ w \ s] *)/ – user3678471

+0

응답은 비슷합니다. – user3678471

+0

이것은 링크 http : // code입니다. tutsplus.com/tutorials/ruby-for-newbies-regular-expressions--net-19812 – user3678471