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

This node was taken out by the NodeReaper on Feb 13, 2015 at 21:56 UTC
  • Comment on Reaped: regex to capture only lower case

Replies are listed 'Best First'.
Re: regex to capture only lower case
by AnomalousMonk (Archbishop) on Feb 13, 2015 at 21:50 UTC
    /a-z.+\.txt/

    If  /[a-z].+\.txt/ is the regex you're using (a bit difficult to tell because you don't use  <code> ... </code> tags), then it will match a string with a lower-case letter anywhere in it, followed by one or more of anything except a newline, followed by  '.txt' and then followed by anything. I recommend using string anchor assertions (untested):
        my @files = grep m{ \A [a-z] .* \.txt \z }xms, readdir $dh;
    (which also allows for single-letter file names). Please see perlre, perlrequick, and perlretut.


    Give a man a fish:  <%-(-(-(-<

Re: regex to capture only lower case
by adamcpfeiffer (Novice) on Feb 13, 2015 at 19:38 UTC
    I believe the issue is that you are not denoting that a-z is a sequence. See:
    http://perldoc.perl.org/perlre.html
    Try this:
    @files = grep ( /[a-z].+\.txt/, readdir($dh));
    Please note, this will not match filenames with a single letter. If you intend to match on a file name a.txt, then you need to chnge the + to *.
    Hope that helps.