 <p>There are well-tested wheels used to tear apart URLs into the component parts so use them. Ruby comes with <a href="http://ruby-doc.org/stdlib-2.3.0/libdoc/uri/rdoc/URI.html" rel="nofollow">URI</a>, which allows us to easily extract the <code>host</code>, <code>path</code> or <code>query</code>:</p> <pre><code>require 'uri' URL = 'http://foo.com/a/b/c?d=1' URI.parse(URL).host # =&gt; "foo.com" URI.parse(URL).path # =&gt; "/a/b/c" URI.parse(URL).query # =&gt; "d=1" </code></pre> <p>Ruby's <a href="http://ruby-doc.org/core-2.3.0/Enumerable.html" rel="nofollow">Enumerable</a> module includes <a href="http://ruby-doc.org/core-2.3.0/Enumerable.html#method-i-reject" rel="nofollow"><code>reject</code></a> and <a href="http://ruby-doc.org/core-2.3.0/Enumerable.html#method-i-select" rel="nofollow"><code>select</code></a> which make it easy to loop over an array or enumerable object and reject or select elements from it:</p> <pre><code>(1..3).select{ |i| i.even? } # =&gt; [2] (1..3).reject{ |i| i.even? } # =&gt; [1, 3] </code></pre> <p>Using all that you could check the host of a URL for sub-strings and reject any you don't want:</p> <pre><code>require 'uri' %w[ http://www.speedtest.net/ http://webcache.googleusercontent.com/search%3Fhl%3Den%26biw%26bih%26q%3Dcache:M47_v0xF3m8J ].reject{ |url| URI.parse(url).host[/googleusercontent\.com$/] } # =&gt; ["http://www.speedtest.net/"] </code></pre> <p>Using these methods and techniques you can reject or select from an input file, or just peek into single URLs and choose to ignore or honor them.</p>