所以这是一个奇怪的问题。我有大量由“主”脚本执行的脚本,我需要验证“主”中的内容是否有效。问题是,这些脚本包含特殊字符,我需要匹配它们以确保“Master”引用正确的脚本。
一个文件的示例可能是
Example's of file names (20160517) [test].sh
这是我的代码的样子。 @MasterScipt
是一个数组,其中每个元素都是我期望子脚本命名的文件名。
opendir( DURR, $FileLocation ); # I'm looking in a directory where the subscripts reside
foreach ( readdir(DURR) ) {
for ( my $j = 0; $j != $MasterScriptlength; $j++ ) {
$MasterScipt[$j] =~ s/\r//g;
print "DARE TO COMPARE\n";
print "$MasterScipt[$j]\n";
print "$_\n";
#I added the \Q to quotemeta, but I think the issue is with $_
#I've tried variations like
#if(quotemeta($_) =~/\Q$MasterScipt[$j]/){
#To no avail, I also tried using eq operator and no luck :(
if ( $_ =~ /\Q$MasterScipt[$j]/ ) {
print "WE GOOD VINCENT\n";
}
}
}
closedir(DURR);
无论我做什么,我的输出总是这样
DARE TO COMPARE
Example's of file names (20160517) [test].sh
Example's of file names (20160517) [test].sh
最佳答案
好吧,我盯着这个东西太久了,我想把这个问题写出来可以帮助我回答它。
我不仅需要在正则表达式中添加 \Q
,而且还有一个空格字符。我对 $_
和 $MasterScipt[$j]
进行了 chomp
,现在它可以工作了。
关于regex - 尝试在 Perl 中匹配两个都包含特殊字符的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37290139/