raddox has asked for the wisdom of the Perl Monks concerning the following question:

First off I must say that I am very thankful that a site like this exist! Plenty of great help and information out here. Now on to my question. I want to use perl grep to pull information from an array. The array contains a list of files with the following name schema XXXX.BTS.200902021400.XXX_XXXXpm. Now when I create the grep statement.
opendir(DIR,"$inputDir"); my @dirList = readdir(DIR); closedir(DIR); my @list = grep /*$classType.$yearMonth????00.XXX_XXXXpm/, @dirList;

Now I am not looking to match the first four characters, the match would start from BTS until the end of the file. the $classType variable would contain BTS, $yearMonth would contain 200902. The four questions marks are to match any date/day and hour and the "00" is there to match for minutes. Now for the life of me I cannot figure out how to build this regular expression so that it will search for "BTS.200902????00.XXX_XXXXpm".
Any help that you guys could provide would be greatly appreciated. I just need a point in the right direction.
Thanks again.
  • Comment on Grepping from an Array using a variable as the pattern to be matched
  • Download Code

Replies are listed 'Best First'.
Re: Grepping from an Array using a variable as the pattern to be matched
by ikegami (Patriarch) on Feb 05, 2009 at 16:49 UTC

    perlre, perlretut
    "." means any character.

    /\.\Q$classType\E\.\Q$yearMonth\E.{4}00\.XXX_XXXXpm\z/
    or
    /^.{4}\.\Q$classType\E\.\Q$yearMonth\E.{4}00\.XXX_XXXXpm\z/
Re: Grepping from an Array using a variable as the pattern to be matched
by linuxer (Curate) on Feb 05, 2009 at 20:00 UTC

    ikegami already linked to the docs and provided two solutions.

    I just want to point out, that you are using the quantifier * as first character in your expression, which can't work, because a quantifier must follow another expression (character, character class, group, ...). Refer to the docs for more details.

    $ perl -wle ' q~hello~ =~ m/*ello/ and print "yeah";' Quantifier follows nothing in regex; marked by <-- HERE in m/* <-- HER +E ello/ at -e line 1. $ perl -wle ' q~hello~ =~ m/.*ello/ and print "yeah";' yeah