诀窍是 - 创建一个正则表达式,并使用 | 创建一个或条件。
所以使用你的:
my @excludepaths = (
"abc/def/",
"hij/klm/",
);
像这样把它变成一个正则表达式:
my $regex = join ( "|", map { quotemeta } @excludepaths );
$regex = qr/($regex)/;
那你应该可以了
next if m/$regex/;
例如:
my @excludepaths = (
"abc/def/",
"hij/klm/",
);
my $regex = join ( "|", @excludepaths );
$regex = qr/($regex)/;
for ( "abc/def/ghk", "abf/de/cg", "abf/hij/klm/ghf", "fish/bat/mix" ) {
next if m/$regex/;
print;
print "\n";
}
如果您这样做,您可以将您喜欢的任何模式添加到您的“排除”中,只需将其添加到列表中即可。
所以你可以添加/foo/.*/Hello$,它会跳过匹配:
/some/path/to/foo/and/more/Hello
因为正则表达式路径是子字符串匹配。
编辑:根据您的 cmets:
my @excludepaths = ( "abc/def/", "hij/klm/", "/foo/", );
my $regex = join( "|", @excludepaths );
$regex = qr/($regex)/;
my $include_regex = qr,/foo/.*\bHELLO$,;
for (
"abc/def/ghk", "abf/de/cg",
"abf/hij/klm/ghf", "fish/bat/mix",
"/path/with/foo/not/HELLO", "/path/with/foo/",
"/path/with/foo/HELLO"
)
{
next if ( m/$regex/ and not m/$include_regex/ );
print;
print "\n";
}
我们明确排除包含/foo/ 的任何内容,但使用$include_regex 覆盖,这样/path/with/foo/not/HELLO 仍会通过文件管理器。