in reply to Regex for files

all the files that begin with messages and MAY have a "." and a digit behind it, but it should not match something that has say .txt, .pl, etc.

Having a directory populated with these files

messages messages. messages.1 messages1.2.3 messages.1.gz messages.2 messages.3 messages.pl messages.txt

the following code

opendir D, '.'; while(readdir D) { /^messages(?:\.\d?)?$/ and print $_,"\n"; }

will output those:

messages. messages.2 messages messages.1 messages.3

The elements of that regular expression can be best explained using the x modifier (see perlre and perlfunc):

opendir D, '.'; while(readdir D) { m/ # begin of match ^ # match at the beginning of element, i.e. $_ messages # match 'messages' literally ( # begin of match group ?: # subject to * ? + not creating a capture \. # match a period \d? # optionally (?) match a digit ) # end of match group ? # which may or not occur $ # before the end of the string /x # tell m// that we use extended regex w/comments and # if found print "$_\n" # print it out. }
perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

Replies are listed 'Best First'.
Re^2: Regex for files (path::tiny)
by Anonymous Monk on Apr 23, 2015 at 11:00 UTC

    readdir is low level :) Path::Tiny is a whole nother level :P

    use Path::Tiny qw/ path /; print "$_\n" for path('.')->children( qr /^messages(?:\.\d?)?$/ ) ;
      readdir is low level :) Path::Tiny is a whole nother level :P

      That may well be, but is irrelevant for Regex for files.

      perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

        readdir is low level :) Path::Tiny is a whole nother level :P

        That may well be, but is irrelevant for Regex for files.

        In what way?