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. }
In reply to Re: Regex for files
by shmem
in thread Regex for files
by bmcquill
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |