[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Need regex (PCRE) for parsing root domains

Posted on 2011-10-09
3
Medium Priority
?
298 Views
Last Modified: 2012-05-12
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.
0
Comment
Question by:hvs69
  • 2
3 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36939363
How about:

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

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36939368
Correction:

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

Open in new window

0
 
LVL 35

Accepted Solution

by:
Terry Woods earned 1000 total points
ID: 36939477
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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question