1

我需要比较 Perl 中的两个 URL。

仅由 DOMAIN 或 DOMAIN/(根 URL 以斜杠结尾)组成的 URL 是等效的。

也就是说,以下字符串应该相等:“http://example.com”和“http://example.com/”。

4

2 回答 2

5

比较

To compare two URLs, use the eq( ) method:    
if ($url_one->eq(url_two)) { ... }

For example:

use URI;
my $url_one = URI->new('http://www.example.com');
my $url_two = URI->new('http://www.example.com/');

if ($url_one->eq($url_two)) {
  print "The two URLs are equal.\n";
}
The two URLs are equal.
于 2012-10-22T11:15:09.017 回答
0

You could do:

my $url_1 = 'http://www.example.com';
my $url_2 = 'http://www.example.com/';

if ( $url_1 =~ m/\A$url_2\/\z/ || $url_2 =~ m/\A$url_1\/\z/ ) {
    print "URLs are the same\n";
}

Well given that they are really valid URLs. The regex only checks that if you add the "/" at the end, it's still the same URL

So if you add "/" at the end of $url_1, it will be 'http://www.example.com/' which will make the second condition in the if true.

于 2012-10-23T15:20:52.873 回答