tags:

views:

445

answers:

5

I have a bunch of domain names comining in like this:

http://subdomain.example.com (example.com is always example.com, but the subdomain differs).

I need "subdomain".

Could someone with regex-fu help me out?

+1  A: 

Purely the subdomain string (result is $1):

^http://([^.]+)\.domain\.com

Making http:// optional (result is $2):

^(http://)?([^.]+)\.domain\.com

Making the http:// and the subdomain optional (result is $3):

(http://)?(([^.]+)\.)?domain\.com
Factor Mystic
+4  A: 
/(http:\/\/)?(([^.]+)\.)?domain\.com/

Then $3 (or \3) will contain "subdomain" if one was supplied.

If you want to have the subdomain in the first group, and your regex engine supports non-capturing groups (shy groups), use this as suggested by palindrom:

/(?:http:\/\/)?(?:([^.]+)\.)?domain\.com/
Draemon
Or /(?:http://)?(?:([^.]+)\.)?domain.com/ and $1 will contain the subdomain
palindrom
True. He didn't mention language/library so I wanted to make the regex as portable as possible - not sure if all implementations allow non-capturing groups.
Draemon
+1  A: 

It should just be

\Qhttp://\E(\w+)\.domain\.com

The sub domain will be the first group.

Jeremybub
A: 

1st group of

http://(.*).example.com
czuk
Forgetting, of course, that `.*` will match an empty string and, more importantly, that the period stands for **any character**.
Sinan Ünür
A: 
#!/usr/bin/perl

use strict;
use warnings;

my $s = 'http://subdomain.example.com';
my $subdomain = (split qr{/{2}|\.}, $s)[1];

print "'$subdomain'\n";
Sinan Ünür