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

Fellow Monks:
I need help 2 fold with regular expressions, for I realize they are exceptionally useful, but, unfortunately even the simplest aspects, as seen in my code snippet below, confound me.
First: What I would like to do is conduct a directory listing, (not a problem), and match into a directory via grep files that meet only the following criteria: match the front part of the filename, a fixed string, and the back part of a filename, essentially a fixed extention; where AAAAAnnnnn.txt; AAAAA is the front half of the filename that is fixed, to be matched; nnnnn is the part of the filename the changes and .txt is the fixed extension.
the code attempt (a snippet):
my $datadir="/user1/localfiles/data/" opendir LOCALDIR, "$datadir" or die "unable to open $datadir\a\n"; my @localfiles=grep/(^file-15a_62_200405)&&(.txt$)/i readdir LOCALDIR; foreach my $file(@localfiles) { print "$file\n"; }
shown above is my blind (lack of knowledge) attempt which obvoiusly didn't work. Originally the grep line was just set to match just the extension,
my @localfiles=grep/.txt$/i readdir LOCALDIR;
or I could match the beginning with:
my @localfiles=grep/^file-15a_62_200405/i readdir LOCALDIR;
which worked just fine. In a nutshell.... I simple do not know how to combine the two. I would really appreciate your help in this matter and I humbly thank you in advance.
The Second Part:
My first thought was to look for tutorials on regular expressions, I googled and found a list longer than my arm. Are there any recommended tutorials for regular expressions favored among the community? I am looking for simplicity and practical application of regexes..... something to start me out from the very beginning.... again many thanks.

Replies are listed 'Best First'.
Re: Simple Regular Expression Help Needed
by tlm (Prior) on May 24, 2005 at 16:03 UTC

    my @localfiles=grep/^file-15a_62_200405.*\.txt$/i readdir LOCALDIR;
    will do ya.

    In this case the simplest thing is to write a regexp that includes both requirements. You do this by separating the conditions at both ends with an "anything goes" linker; that's what the .* bit is all about. Also note that parentheses are not needed in this case.

    Sometimes it happens that it is simplest to use two or more separate regexp tests, which can still be combined in a single grep, like this:

    my @localfiles=grep /^file-15a_62_200405/i && /\.txt$/i readdir LOCALD +IR;

    For a tutorial try perlretut.

    Update:Added explanatory remarks.

    the lowliest monk

Re: Simple Regular Expression Help Needed
by thundergnat (Deacon) on May 24, 2005 at 16:08 UTC

    You should get better results if you change the regex in your grep to

    my @localfiles=grep/^file-15a_62_200405.+\.txt$/i readdir LOCALDIR;

    As for a regex tutorial,

    perldoc perlretut

    should give you a pretty good start.

Re: Simple Regular Expression Help Needed
by prasadbabu (Prior) on May 24, 2005 at 16:03 UTC

    How about this,

    my @localfiles=grep(/(^file-15a_62_200405)/i && /(\.txt$)/i) readdir LOCALDIR;

    Prasad

Re: Simple Regular Expression Help Needed
by northwind (Hermit) on May 24, 2005 at 16:04 UTC

    Try /^file-15a_62_200405.*?\.txt$/i

      Just as a side note here - that ? to make the match non-greedy. You have anchors at both ends (^ $), so greediness shouldn't come into play here. You actually do want to match as much as possible.

      Yeah, I know. Really minor. :-)

        Yep, you're right.  For whatever reason, I had non-backtracking patterns on the brain even though there is nothing in the regex that states it is non-backtracking...  And the $ shouldn't be there, it was a blind copy/paste from OP's code.