匹配所有日志文件的正则表达式匹配一种模式

时间:2014-06-04 11:13:18

标签: regex perl

我正在尝试编写一个匹配所有后续文件名的正则表达式。

acbd1251-sample-tmp-store-tmp-1-application.log
acbd1251-sample-tmp-store-tmp-2-application.log
acbd1251-sample-tmp-store-tmp-3-application.log
acbd1251-sample-zmp-store-zmpstore-01-application.log
acbd1251-sample-zmp-store-zmpstore-02-application.log

我尝试按照RegEx来匹配上述文件。

$logfile =~ /(\w+)-sample-(tmp|zmp)-store-(tmp|zmpstore)-(\d+)-application.log;

当我运行以下脚本时,它不会打印任何内容。

#!/usr/bin/perl 
my $logfile =~ /(\w+)-sample-(tmp|zmp)-store-(tmp|zmpstore)-(\d+)-application.log/;
my $dir = "C:/test/$logfile";

@files = glob( $dir );
foreach (@files ){
   print $_ . "\n";
}

我错过了什么?

1 个答案:

答案 0 :(得分:2)

如果你想定义正则表达式,那么

my $logfile = qr/(\w+)-sample-(tmp|zmp)-store-(tmp|zmpstore)-(\d+)-application.log/;

use warnings会告诉您Use of uninitialized value $logfile in pattern match

my $logfile =~ /(\w+)-sample-(tmp|zmp)-store-(tmp|zmpstore)-(\d+)-application.log/;

最后你想要过滤/ grep globed目录,

my @files = grep /$logfile/, glob( "C:/test" );