2016-06-26 15 views
0

私は、exのためリンク@からテキストを抽出するには?

@post.link 

からテキストを抽出したいです。 「http://test.com/c/849324

は私が

@v = @post.link.read(/c/) 

/C/

後にリンクを読み取るために、このコードを使用するが、私はこのエラーが

undefined method `read' for 

たぶん、この場合

のために使用する正しい方法ではありません取得します

編集: 「http://test.com/c/849324」から「849324」を取得します

+0

ページからテキストを抽出したり、URLを分割しようとしていますか? – danielrsmith

+0

URLを分割します。< – Ghamtre

答えて

0

は、文字列のインデックスから直接正規表現を使用することができます@post.link.split('/c/').last

0

を試してみてください。

first_group = string[/text(grp)/, 1] # get content of captured group 

から:https://github.com/bbatsov/ruby-style-guide#regular-expressions


例使用IRB:

irb(main):002:0> require 'uri' 
=> true 
irb(main):003:0> URI("http://test.com/c/849324").path 
=> "/c/849324" 
irb(main):004:0> URI("http://test.com/c/849324").path[/^\/c\/(.*)/, 1] 
=> "849324" 

回答:

URI("http://test.com/c/849324").path[/^\/c\/(.*)/, 1] 
+0

ええ、リンクは@ post.link内にあります – Ghamtre

+0

'URI(@ post.link).path [/^\/c \ /(。*)/、1]' – SoAwesomeMan

+0

URLが変更されない場合(例えば、開発環境、ステージング環境、本番環境の場合)、 '@ post.link [/^http:\/\/test \ .com \/c \ /(。*)/] 1] ' – SoAwesomeMan

関連する問題