You should look at [`URI.extract`](http://ruby-doc.org/stdlib-2.0.0/libdoc/uri/rdoc/URI.html#method-c-extract). Perfect for this.