Perl Regex to get the root domain of a URL

后端 未结 6 1910
不思量自难忘°
不思量自难忘° 2021-01-13 17:40

How could I get some part of url?

For example:

http://www.facebook.com/xxxxxxxxxxx
http://www.stackoverflow.com/yyyyyyyyyyyyyyyy

I

6条回答
  •  悲哀的现实
    2021-01-13 18:24

    use feature qw( say state );
    
    use Domain::PublicSuffix qw( );
    use URI                  qw( );
    
    # Returns "domain.tld" for "subdomain.domain.tld". 
    # Handles multi-level TLDs such as ".co.uk".
    sub root_domain {
       my ($domain) = @_;
       state $parser = Domain::PublicSuffix->new();
       return $parser->get_root_domain($domain);
    }
    
    # Accepts urls as strings and as URI objects.
    sub url_root_domain {
       my ($abs_url) = @_;
       my $domain = URI->new($abs_url)->host();
       return root_domain($domain);
    }
    
    say url_root_domain('http://www.facebook.com/');       # facebook.com
    say url_root_domain('https://www.facebook.com/');      # facebook.com
    say url_root_domain('http://mobile.google.com/');      # google.com
    say url_root_domain('http://www.theregister.co.uk/');  # theregister.co.uk
    say url_root_domain('http://www.com/');                # www.com
    

提交回复
热议问题