2016-12-18 12 views
0

正規表現でURLをチェックします。正規表現のURL変換に不要な文字が含まれています

本文に含まれるURLをhtmlに処理します。

不要なチャプターが入るので、不要なチャプターを入れたくありません。

私の正規表現は次のとおりです。

body 
=> "https://www.yahoo.com/<br /><br />sample<br /><br/>https://www.yahoo.com/" 
url 
=>"https://www.yahoo.com/" 
text 
=> "<!-- BEGIN app/views/topics/_link_thumbnail_description.html.slim -->\n\n<a class=\"c-grid__quotation--link\" target=\"_blank\" href=\"https://www.yahoo.com/\"><div class=\"c-grid__quotation text--s-md p-topic__quotation__border c-border-r-5\">\n <div class=\"c-flex\">\n <div class=\"c-grid__quotation--main\">\n  <img src=\"https://s.yimg.com/dh/ap/default/130909/y_200_a.png\" alt=\"Y 200 a\" />\n </div>\n <div class=\"c-grid__quotation--side\">\n  <div class=\"c-grid__quotation--side-title text--b\">\n  Yahoo\n  </div>\n  <div class=\"c-grid__quotation--side-description\">\n  News, email and search are just the beginning. Discover more every day. Find your yodel.\n  </div>\n  <div class=\"c-grid__quotation--side-url\">\n  www.yahoo.com\n  </div>\n </div>\n </div>\n</div></a><!-- END app/views/topics/_link_thumbnail_description.html.slim -->" 


    def convert_url_to_text(body, url, text) 
    reg_url = Regexp.escape("#{url}") 
    body.gsub!(/(#{reg_url}$|#{reg_url}[\W\/])/){ |s| "#{text}"} 
    end 

正規表現の状態になります。

/(https:\/\/www\.yahoo\.com\/$|https:\/\/www\.yahoo\.com\/[\W\/])/ 

しかし、URL体int型<

http://rubular.com/

を取得するにはどうすればいない<含まれていないことができますか?

答えて

2

手動で解析しないでください。 URI#extractを使用:

URI.extract "https://www.yahoo.com/<br /> 
    <br />sample<br /><br/>https://www.yahoo.com/" 
#⇒ ["https://www.yahoo.com/", "https://www.yahoo.com/"] 
関連する問題