title: regex match main domain name
body: <p>I need to be able to identify a domain name of any subdomain.</p>
  
  <p>Examples:</p>
  
  <p>For all of thiese I need to match only <code>example.co</code> / <code>example.com</code> / <code>example.org</code> / <code>example.co.uk</code> / <code>example.com.au</code> / <code>example.gov.us</code> and so on</p>
  
  <pre><code>www.example.co
  www.first.example.co
  first.example.co
  second.first.example.co
  no.matter.how.many.example.co
  first.example.co.uk
  second.first.example.co.uk
  no.matter.how.many.example.co.uk
  first.example.org
  second.first.example.org
  no.matter.how.many.example.org
  first.example.gov.uk
  second.first.example.gov.uk
  no.matter.how.many.example.gov.uk
  </code></pre>
  
  <p>I have been playing with regular expressions all day and been Googleing for something all day long and still can't seem to find something.</p>
  
  <p><strong>Edit2: I prefer a regex that might fail for very odd cases like t.co then list all TLD's and have the ones I did not list but could have been predicted fail and match more then it should. Isn't this be the option you would chose?</strong></p>
  
  <p>Update:
  Using the chosen answer as a guide I have constructed this regex that does the job for me.</p>
  
  <p><code>/([0-9a-z-]{2,}\.[0-9a-z-]{2,3}\.[0-9a-z-]{2,3}|[0-9a-z-]{2,}\.[0-9a-z-]{2,3})$/i</code></p>
  
  <p><em>It might not be perfect but so far I have not encountered a case where it fails.</em></p>