我有一个带有一些值的数组......
@array=("B_MY03", "D16", "DECAP2");
如何使用每个值在文件中查找包含该值的模式?
while(<$file>){
foreach $val(@array){
if( $_ =~ /^item\s\=\s\w+("GR\$key\",/){...}
}
}
模式看起来像这样:
Item = Type("GRB_MY03", "DI");
答案 0 :(得分:4)
my @array = ("B_MY03", "D16", "DECAP2");
my ($re) = map qr/$_/, join "|", map quotemeta, @array;
while(<$file>) {
# Item = Type("GRB_MY03", "DI");
if ( /^Item\s*=\s*\w+\("GR($re)"/ ) { ... }
}
答案 1 :(得分:1)
use strict;
use warnings;
sub list_to_regexp {
local $" = q(|);
my @list = map quotemeta, @_;
return qr/@list/;
}
my @values = ('value1', 'value2', 'value3');
my $values_re = list_to_regexp(@values);
while (<DATA>)
{
if (/^ item \s = \s ($values_re) $/x)
{
print "On line $. of data; found item '$1'\n";
}
}
__DATA__
item = value3
item = value4
item = value1
primate = monkey
primate = orangutan
item = value1
primate = chimpanzee