2009-07-27 17 views
10

Ho un sacco di nomi di dominio in arrivo in questo modo:Regex per estrarre il sottodominio dall'URL?

http://subdomain.example.com (example.com è sempre example.com, ma il sottodominio varia).

Ho bisogno di "sottodominio".

Una persona gentile che ha avuto la pazienza di imparare regex potrebbe aiutarmi?

+1

È consentito il sottotubo? –

+0

Sì, si può avere string.string.domain.gtld –

risposta

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

Quindi $ 3 (o \ 3) conterrà "sottodominio" se ne è stato fornito uno.

Se si desidera avere il sottodominio nel primo gruppo e il motore regex sostiene i gruppi non-cattura (gruppi timidi), utilizzare questo come suggerito da Palindrom:

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

O /(?:http://)?(?:([^.]+)\.)?domain.com/ e $ 1 conterrà il sottodominio – palindrom

+0

Vero. Non ha menzionato la lingua/libreria, quindi ho voluto rendere l'espressione regolare il più portabile possibile - non sono sicuro che tutte le implementazioni consentano i gruppi che non catturano. – Draemon

+0

E se non sapessi cosa sia 'domain'? –

4

Puramente la stringa sottodominio (risultato è di $ 1):

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

Fare http:// opzionale (risultato è di $ 2):

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

Rendere il http:// e il sottodominio opzionale (risultato è di $ 3):

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

Dovrebbe essere solo

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

Il sottodominio sarà il primo gruppo.

0

1 ° gruppo di

http://(.*).example.com 
+1

Dimenticando, ovviamente, che '. *' Corrisponderà a una stringa vuota e, cosa più importante, che il punto sta per ** qualsiasi carattere **. –

0
#!/usr/bin/perl 

use strict; 
use warnings; 

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

print "'$subdomain'\n"; 
29

Il problema con la regex di cui sopra è: se non si sa che cosa il protocollo è, o ciò che il suffisso di dominio è, si ottengono alcuni risultati inaspettati. Ecco alcuni account regex per quelle situazioni. : D

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

Questo dovrebbe sempre restituire il sottodominio (se presente) nel gruppo 1. Qui è in un esempio di Javascript, ma dovrebbe funzionare anche per qualsiasi altro motore che supporta positive affermazioni look-ahead:

// 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 
    } 
} 
+3

questa è chiaramente la migliore risposta perché rappresenta il protocollo, nessuno/più sottodomini ed è indipendente dal dominio. – mastaBlasta

+2

Lavoro fenomenale! – plast1K

+2

Questa è la risposta migliore e dovrebbe essere assolutamente accettata. –

Problemi correlati