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?
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?
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
/(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/
It should just be
\Qhttp://\E(\w+)\.domain\.com
The sub domain will be the first group.
#!/usr/bin/perl
use strict;
use warnings;
my $s = 'http://subdomain.example.com';
my $subdomain = (split qr{/{2}|\.}, $s)[1];
print "'$subdomain'\n";