Regex Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreRegex Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 11th, 2009, 03:40 AM
murklys murklys is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2008
Posts: 496 murklys User rank is Second Lieutenant (5000 - 10000 Reputation Level)murklys User rank is Second Lieutenant (5000 - 10000 Reputation Level)murklys User rank is Second Lieutenant (5000 - 10000 Reputation Level)murklys User rank is Second Lieutenant (5000 - 10000 Reputation Level)murklys User rank is Second Lieutenant (5000 - 10000 Reputation Level)murklys User rank is Second Lieutenant (5000 - 10000 Reputation Level)murklys User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 3 Days 21 h 59 m 29 sec
Reputation Power: 87
Extract domain from a hostname

Hi,

I'd like to extract the domain from a given hostname. For example:

hostname: this.is.a.hostname.domain.com
result: domain.com

hostname: domain.com
result: domain.com

My current regexp (php) is:

/([a-zA-Z0-9\-_]+\.[a-zA-Z0-9\-_]{2,3})$/

I'm very bad at regexp so i need some help. This current example works for domains that end in com, ru or something that contains 2 or 3 characters. But how do i specify that the ending of the domain can be:
either: 2 or 3 characters long
or: 2 or 3 characters long, followed by a dot, followed by to or 3 more characters (like co.uk)

How should i modify this expression?

Also there's a problem of knowing if the co.uk part is actually a domain ending or is it adomain itself (like ab.com).

Last edited by murklys : September 11th, 2009 at 03:47 AM.

Reply With Quote
  #2  
Old September 13th, 2009, 04:26 PM
liljim's Avatar
liljim liljim is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jul 2001
Location: England
Posts: 967 liljim User rank is Lance Corporal (50 - 100 Reputation Level)liljim User rank is Lance Corporal (50 - 100 Reputation Level)liljim User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 20 h 32 m 5 sec
Reputation Power: 12
I'm a little rusty with my regex and there might be something simpler you could use, but try:

/(([a-z0-9]+)(-[a-z0-9]+)?)(\.[a-z]{2}\.[a-z]{2}|\.[a-z]{2,4})$/i

Also, AFAIK, domain names don't typically include underscores. You're out of luck if you're trying to match ip addresses with this expression, or if the user ends the domain with / or whatever. I don't really know where you're getting your data from, though, so it's difficult to say whether or not this will be a problem for you.

I tried this against the following (some of which are random domains, some real), and the expression worked out against all of them:

domain.name
domain.com
domain.ch
domain.co.uk
domain-name.com
my-domain-name.com
123-reg.co.uk
www.123-reg.co.uk
ab.com
my.really.obscure.domain.com
http://www.google.com

Reply With Quote
  #3  
Old September 14th, 2009, 10:57 AM
ManiacDan's Avatar
ManiacDan ManiacDan is offline
Likely to be eaten by a grue.
Dev Shed God 10th Plane (9500 - 9999 posts)
 
Join Date: Oct 2006
Location: Pennsylvania, USA
Posts: 9,791 ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)  Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 2 Months 3 Weeks 14 h 53 m 20 sec
Reputation Power: 6112
You could just use parse_url. Remember, in PHP, there's almost always a built-in function for what you're trying to do.

-Dan
__________________
HEY! YOU! Read the New User Guide and Forum Rules

"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

"The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreRegex Programming > Extract domain from a hostname

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap