amicngh
amicngh

Reputation: 7899

Regex to match all log files matches one pattern

I am trying to write one regex which matches all following filename.

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

I have tried following RegExto match above files.

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

When I run following script it is not printing anything.

#!/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";
}

What am I missing ?

Upvotes: 0

Views: 218

Answers (1)

mpapec
mpapec

Reputation: 50677

If you want do define regex then,

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

use warnings would tell you Use of uninitialized value $logfile in pattern match for

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

and finally you'll want to filter/grep globed directory,

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

Upvotes: 2

Related Questions