我需要比较 Perl 中的两个 URL。
仅由 DOMAIN 或 DOMAIN/(根 URL 以斜杠结尾)组成的 URL 是等效的。
也就是说,以下字符串应该相等:“http://example.com”和“http://example.com/”。
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.
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.