给定变量:
my $var1 = "fish";
my $var2 = "ocean";
我的输入字符串可以是fishin_ocean或fishout_ocean。
如何编写一个 Perl 正则表达式来匹配两个输入字符串,其中正则表达式的部分在$var1
和中$var2
?这个(字符串所在的位置$var3
)不返回上述任何一个字符串:
$var3 =~ /$var1.+?$var2/;
从OP的答案编辑:
我认为是最后的特殊方括号导致了问题
my $a = "slash_wideiscan"; my $b = "_mnn3h_reg[0]";
my @array1 = qw (slash_wideiscanps_mnn3h_reg[0]
slash_wideiscanptlr_mnn3h_reg[0]
slash_wideiscanps_mnn0h_reg);
for my $element (@array1) {
if ($element =~ /($a.+?$b)/) {
print "YES : $element\n";
} else {
print "NO : $element\n";
}
}
回来的答案是
NO : slash_wideiscanps_mnn3h_reg[0]
NO : slash_wideiscanptlr_mnn3h_reg[0]
NO : slash_wideiscanps_mnn0h_reg
我其实想要
YES : slash_wideiscanps_mnn3h_reg[0]
YES : slash_wideiscanptlr_mnn3h_reg[0]
NO : slash_wideiscanps_mnn0h_reg