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. }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Regex for files (path::tiny)
by Anonymous Monk on Apr 23, 2015 at 11:00 UTC | |
by shmem (Chancellor) on Apr 23, 2015 at 12:16 UTC | |
by Anonymous Monk on Apr 23, 2015 at 15:38 UTC | |
by shmem (Chancellor) on Apr 23, 2015 at 21:09 UTC | |
by Anonymous Monk on Apr 24, 2015 at 00:23 UTC |