Perl Regex to get the root domain of a URL

后端 未结 6 1957
不思量自难忘°
不思量自难忘° 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:14

    Just some simple regex stuff.

    $facebook = "www.facebook.com/xxxxxxxxxxx";
    
    $facebook =~ s/www\.(.*\.com).*/$1/; # get what is between www. and .com
    
    print $facebook;
    

    Returns

    facebook.com
    

    You may also want to make this work for .net, .org, etc. Something like:

    s/www\.(.*\.(?:net|org|com)).*/$1/;
    

提交回复
热议问题