在Perl中使用参数作为正则表达式

时间:2014-02-03 12:42:31

标签: regex perl parameter-passing

我正在使用正则表达式作为我脚本的参数。就像/stat.*\.log/

一样

我尝试将它与Perl的内置grep一起使用。

my @dots = readdir($dh);
@dots=grep($conf->{classifier_regx}, @dots);

其中@dots是包含文件和子文件夹的完整目录。 $ conf-> {classifer_regx}是给出参数的正则表达式。

实际上,它会返回所有文件和子文件夹。是否有可能在运行时grep值?我怎样才能做到这一点?我错过了什么?手册页?

2 个答案:

答案 0 :(得分:3)

我认为您//中缺少grep

@dots = grep(/$conf->{classifier_regx}/, @dots);

如果仍然无效,可以尝试

my $value = $conf->{classifier_regx};
@dots = grep(/$value/, @dots);

答案 1 :(得分:-2)

如果您使用Bash脚本,则更容易,因为您可以在脚本上使用grep。但是,由于您使用的是Perl,因此可以执行以下操作:

my @dots = readdir($dh);

$str = <STDIN>; //This will allow you to enter a string at runtime
chomp str;

foreach $i (@dots) { // Iterate through the array. If a value is found, print it
    if ($i eq $str) {
        print "$str";
    }
}