如何使用Ruby on Rails从电子邮件中提取所有URls /链接?

时间:2011-05-02 07:34:23

标签: ruby-on-rails ruby ruby-on-rails-3

我正在建立一个书签网站。我想从电子邮件中提取所有URI /链接。我的网站正在使用Ruby on Rails。

如何提取收到的电子邮件内容的所有网址?

2 个答案:

答案 0 :(得分:11)

Ruby的内置URI模块已经这样做了:

来自extract文档:

require "uri"

URI.extract("text here http://foo.example.org/bla and here mailto:test@example.com and here also.")
# => ["http://foo.example.com/bla", "mailto:test@example.com"]

答案 1 :(得分:4)

require 'uri'

text = %{"test
<a href="http://www.a.com/">http://www.a.com/</a>, and be sure
to check http://www.a.com/blog/. Email me at <a href="mailto:b@a.com">b@a.com</a>.}


END_CHARS = %{.,'?!:;}
p URI.extract(text, ['http']).collect { |u| END_CHARS.index(u[-1]) ? u.chop : u }

来源:http://www.java2s.com/Code/Ruby/Network/ExtractURL.htm