title: Regular expression to retrieve domain.tld
body: <p>I'm need a regular expression in Java that I can use to retrieve the domain.tld part from any url. So <a href="https://foo.com/bar" rel="nofollow noreferrer">https://foo.com/bar</a>, <a href="http://www.foo.com#bar" rel="nofollow noreferrer">http://www.foo.com#bar</a>, <a href="http://bar.foo.com" rel="nofollow noreferrer">http://bar.foo.com</a> will all return foo.com.</p>
  
  <p>I wrote this regex, but it's matching the whole url </p>
  
  <pre><code>Pattern.compile("[.]?.*[.x][a-z]{2,3}");
  </code></pre>
  
  <p>I'm not sure I'm matching the "." character right. I tried "." but I get an error from netbeans.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>The tld is not limited to 2 or 3 characters, and <a href="http://www.foo.co.uk/bar" rel="nofollow noreferrer">http://www.foo.co.uk/bar</a> should return foo.co.uk.</p>