URI::regexp
You can generate a regular expression that matches URIs using URI
from Ruby's stdlib:
require 'uri'
matcher = URI.regexp(["http"])
md = matcher.match("I like http://google.com, for real")
#=> #<MatchData "http://google.com" 1:"http" 2:nil 3:nil 4:"google.com" 5:nil 6:nil 7:nil 8:nil 9:nil>
md[0]
#=> "http://google.com"
You can use this with String#scan
to extract all URIs from a string:
urls= []
"I like http://google.com, for real. Also, http://localhost:3000 is useful.".scan(matcher) { urls << Regexp.last_match[0] }
urls
#=> ["http://google.com", "http://localhost:3000"]
See http://ruby-doc.org/stdlib-2.2.2/libdoc/uri/rdoc/URI.html#method-c-regexp for more information.
Tweet