I have to search for a pattern inside a variable in Perl.
The pattern also needs to be inside a variable.
Here is what I have:
opendir(DIR,"reports/");
$poolname = $poolname."_";
@FILES= grep {/^$poolname\.[0-9]*.csv/} readdir(DIR);
@sorted= reverse sort @FILES;
The pattern I want to match is poolname_[0-9]*
(I’m trying to get the latest report for a pool here. [0-9]* is the unix timestamp of the file)
But the above regex is not working as expected.
$sorted[0] doesn’t have the required filename.
May I know what is wrong with the above code?
Assuming that somewhere before the snippet in the question, there is an assignment equivalent to:
then you say you are searching for:
but your regex is searching for:
The patterns you seek will not be matched by your regex; remove the
\.to get the result you require.Given a directory
reportscontaining files:this script:
produces the output:
If that is not what you’re after, your comments and your question are misleading.