Connecting Tech Pros Worldwide Forums | Help | Site Map

Removing subdomains from a string

Newbie
 
Join Date: Aug 2009
Posts: 5
#1: Sep 11 '09
Hi

Could anyone give me some pointers on how to remove subdomains from a string?

For example:
  • If the string was bytes.com, it would stay bytes.com
  • If it was subdomain.bytes.com, it would become bytes.com
  • If it was subsubdomain.subdomain.bytes.com, it would become bytes.com

Essentially, it just needs to strip off the subdomains and produce the root domain if not already present.

Cheers
akadeco

micmast's Avatar
Familiar Sight
 
Join Date: Mar 2008
Location: Belgium
Posts: 136
#2: Sep 11 '09

re: Removing subdomains from a string


I would suggest explode the string and always take the last 2 values of the returned array.
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,563
#3: Sep 11 '09

re: Removing subdomains from a string


To clarify micmast's suggestion, I believe he means split instead of explode.

Expand|Select|Wrap|Line Numbers
  1. >>> s = 'bytes.com'
  2. >>> '.'.join(s.split('.')[-2:])
  3. 'bytes.com'
  4. >>> s = 'subsubdomain.subdomain.bytes.com'
  5. >>> '.'.join(s.split('.')[-2:])
  6. 'bytes.com'
  7. >>> 
micmast's Avatar
Familiar Sight
 
Join Date: Mar 2008
Location: Belgium
Posts: 136
#4: Sep 11 '09

re: Removing subdomains from a string


bvdet, yes you are right :) explode is php syntax.

sorry.
Reply

Tags
python remove string