/^www\.example\.com\/([^\/]+\/)*([^\/]+)\/$/
这是做什么的?
- 匹配域的普通文本。根据需要进行调整。
- 匹配任意数量的目录,每个目录由非斜线字符后跟一个斜线组成。
- 匹配一串非斜杠。
- 匹配输入末尾的斜线,从而消除文件(因为只有目录以斜线结尾)。
在 Perl 中实现:
[ghoti@pc ~] cat perltest
#!/usr/local/bin/perl
@test = (
'www.example.com/path/to/file.html',
'www.example.com/match/',
'www.example.com/pages/match/',
'www.example.com/pages/widgets/thingy/',
'www.example.com/foo/bar/baz/',
);
foreach (@test) {
$_ =~ m/^www\.example\.com\/([^\/]+\/)*([^\/]+)\/$/i;
printf(">> %-50s\t%s\n", $_, $2);
}
[ghoti@pc ~] ./perltest
>> www.example.com/path/to/file.html
>> www.example.com/match/ match
>> www.example.com/pages/match/ match
>> www.example.com/pages/widgets/thingy/ thingy
>> www.example.com/foo/bar/baz/ baz
[ghoti@pc ~]