您可以使用正则表达式来做到这一点,但让URI库为您做这件事的工作要少得多。您不会被路径前后的有趣方案、转义和额外的东西(查询、锚点、授权......)所吸引。path_segments() 表示路径的方式有些棘手。有关详细信息,请参阅下面的评论和URI 文档。
我假设这http://www.example.com/foo/
被认为是顶级目录。根据需要进行调整,但这是您必须考虑的事情。
#!/usr/bin/env perl
use URI;
use File::Spec;
use strict;
use warnings;
use Test::More 'no_plan';
sub is_top_level_uri {
my $uri = shift;
# turn it into a URI object if it isn't already
$uri = URI->new($uri) unless eval { $uri->isa("URI") };
# normalize it
$uri = $uri->canonical;
# split the path part into pieces
my @path_segments = $uri->path_segments;
# for an absolute path, which most are, the absoluteness will be
# represented by an empty string. Also /foo/ will come out as two elements.
# Strip that all out, it gets in our way for this purpose.
@path_segments = grep { $_ ne '' } @path_segments;
return @path_segments <= 1;
}
my @filtered_uris = (
"http://www.example.com/hello.html",
"http://www.example.com/",
"http://www.example.com",
"https://www.example.com/",
"https://www.example.com/foo/#extra",
"ftp://www.example.com/foo",
"ftp://www.example.com/foo/",
"https://www.example.com/foo/#extra",
"https://www.example.com/foo/?extra",
"http://www.example.com/hello.html#extra",
"http://www.example.com/hello.html?extra",
"file:///foo",
"file:///foo/",
"file:///foo.txt",
);
my @unfiltered_uris = (
"http://www.foo.com/this/thingrighthere.html",
"https://www.example.com/foo/bar",
"ftp://www.example.com/foo/bar/",
"file:///foo/bar",
"file:///foo/bar.txt",
);
for my $uri (@filtered_uris) {
ok is_top_level_uri($uri), $uri;
}
for my $uri (@unfiltered_uris) {
ok !is_top_level_uri($uri), $uri;
}