Need regex (PCRE) for parsing root domains

I am looking for a regular expression for a php script to parse root domain from a given string with a 3 character TLD. For example,

www.msn.com
games.msn.com
www.games.msn.com
etc.

should all return msn.com. It should also work with other TLDs like .net, .org etc.

Thanks.
hvs69Asked:
Who is Participating?
 
Terry WoodsIT GuruCommented:
If you want to use the IANA official list of TLDs (which will include 2 part TLDs like .com.au), there's good info in this article on how to do it:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_7830-A-Quick-Tour-of-Test-Driven-Development.html
0
 
käµfm³d 👽Commented:
How about:

(?<=//|\.(?!-))[a-z0-9-]+(?<!-)\.[a-z]{3}\b

Open in new window

0
 
käµfm³d 👽Commented:
Correction:

(?<=//|\.(?!-))[a-z0-9-]+(?<!-)\.[a-z]{3}\b(?!\.)

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.