我正在建立一个书签网站。我想从电子邮件中提取所有URI /链接。我的网站正在使用Ruby on Rails。
如何提取收到的电子邮件内容的所有网址?
答案 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 }