[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 249
  • Last Modified:

yymmdd patern matching


 I'm attempting to find .txt files in a directory whose file names match any date pattern in yymmdd format
 i.e. folder contents with four files:
    file_040103.txt
    060504.zip
    file_060606_xyz.txt
    some_other_file.txt
   
# - code attempt -
opendir(DIR, "c:\\folder");
@files = grep(/[00-99][01-12][01-31].txt/,readdir(DIR));         # find any text file with any yymmdd date pattern
closedir(DIR);

print "@files\n"

--want this result showing the two files--
file_040103.txt
file_060606_xyz.txt

0
Marketing_Insists
Asked:
Marketing_Insists
1 Solution
 
Harisha M GCommented:
try:

@files = grep(/.*\d\d(0\d|1[012])([012]\d|[3][01]).*\.txt/,readdir(DIR));
0
 
ozoCommented:
@files = grep(/(?<!\d)\d\d(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])(?!\d).*\.txt$/,readdir(DIR));
0
 
TintinCommented:
If you aren't too fussy about doing date range validation, you could do:

foreach my $file (<*[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].txt>) {
  print "$file\n";
}
0
 
Marketing_InsistsAuthor Commented:
I managed to use mgh_mgharishs' first.
Thanks!
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now