title: Specific domain URL validation with Regular Expression
body: <p>I've been trying myself, and searching online, to write this regular expression but without success.</p>
  
  <p>I need to validate that a given URL is from a specific domain and a well-formed link (in PHP). For example:</p>
  
  <p>Good Domain: example.com</p>
  
  <p>So good URLs from example.com:</p>
  
  <ul>
  <li><a href="http://example.com/so/this/is/good" rel="nofollow noreferrer">http://example.com/so/this/is/good</a></li>
  <li><a href="http://example.com/so/this/is/good/index.html" rel="nofollow noreferrer">http://example.com/so/this/is/good/index.html</a></li>
  <li><a href="http://www.examle.com/so/this/is/good/mice.html#anchortag" rel="nofollow noreferrer">http://www.examle.com/so/this/is/good/mice.html#anchortag</a></li>
  <li><a href="http://anysubdomain.example.com/so/this/is/good/wow.php" rel="nofollow noreferrer">http://anysubdomain.example.com/so/this/is/good/wow.php</a></li>
  <li><a href="http://anysubdomain.example.com/so/this/is/good/wow.php?search=doozy" rel="nofollow noreferrer">http://anysubdomain.example.com/so/this/is/good/wow.php?search=doozy</a></li>
  </ul>
  
  <p>So bad URLs not from example.com:</p>
  
  <ul>
  <li><a href="http://obviousexample.com" rel="nofollow noreferrer">http://obviousexample.com</a></li>
  <li><a href="http://localhost/example.com/whatever/you/get/the/idea" rel="nofollow noreferrer">http://localhost/example.com/whatever/you/get/the/idea</a></li>
  <li>etc.</li>
  </ul>
  
  <p>Some notes:
  I don't care about "http" verus "https" but if it matters to you assume "http" always
  The code that will use this regex is PHP so extra points for that.</p>
  
  <p><strong>UPDATE 2010:</strong></p>
  
  <p>Gruber adds a great URL regex:</p>
  
  <pre><code>?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()&lt;&gt;]+|\(([^\s()&lt;&gt;]+|(\([^\s()&lt;&gt;]+\)))*\))+(?:\(([^\s()&lt;&gt;]+|(\([^\s()&lt;&gt;]+\)))*\)|[^\s`!()\[\]{};:'".,&lt;&gt;?«»“”‘’]))
  </code></pre>
  
  <p>See his post: <a href="http://daringfireball.net/2010/07/improved_regex_for_matching_urls" rel="nofollow noreferrer">An Improved Liberal, Accurate Regex Pattern for Matching URLs</a></p>