Regex extract subdomain from url?

I have a bunch of domain names as follows:

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

I need a subdomain.

Can any person who had the patience to study regex help me?

+10
source share
6 answers
/(http:\/\/)?(([^.]+)\.)?domain\.com/ 

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

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

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

The problem with the above expression: if you do not know what the protocol is, or what the domain suffix is, you will get unexpected results. Here are some regular expressions for these situations: D

 /(?:http[s]*\:\/\/)*(.*?)\.(?=[^\/]*\..{2,5})/i //javascript 

This should always return your subdomain (if any) in group 1. Here it is shown in the Javascript example, but it should also work for any other engine that supports positive forward statements:

 // EXAMPLE of use var regex = /(?:http[s]*\:\/\/)*(.*?)\.(?=[^\/]*\..{2,5})/i , whoKnowsWhatItCouldBe = [ "www.mydomain.com/whatever/my-site" //matches: www , "mydomain.com"// does not match , "http://mydomain.com" // does not match , "https://mydomain.com"// does not match , "banana.com/somethingelse" // does not match , "https://banana.com/somethingelse.org" // does not match , "http://what-ever.mydomain.mu" //matches: what-ever , "dev-www.thisdomain.com/whatever" // matches: dev-www , "hot-MamaSitas.SomE_doma-in.au.xxx"//matches: hot-MamaSitas , "http://hot-MamaSitas.SomE_doma-in.au.xxx" // matches: hot-MamaSitas , "..ru" //even non english chars! Woohoo! matches:  , ".ru" //does not match ]; // Run a loop and test it out. for ( var i = 0, length = whoKnowsWhatItCouldBe.length; i < length; i++ ){ var result = whoKnowsWhatItCouldBe[i].match(regex); if(result != null){ // YAY! We have a match! } else { // Boo... No subdomain was found } } 
+29
source

Pure subdomain line (the result is $ 1):

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

Creating http:// optional (the result is $ 2):

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

Creating the http:// option and subdomain (the result is $ 3):

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

It should be easy

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

The subdomain will be the first group.

+2
source

1st group

 http://(.*).example.com 
0
source
 #!/usr/bin/perl use strict; use warnings; my $s = 'http://subdomain.example.com'; my $subdomain = (split qr{/{2}|\.}, $s)[1]; print "'$subdomain'\n"; 
0
source

All Articles