use Data::Dumper; ($match) = (@ARGV); # Get the 'printf format' string to match printf(" \$match: >%s<\n\n", $match); # Input as `Img%04d.png` (backprime quoted) $match =~ /([\w]+)(%[0-9]+d)\.([\w]+)/i; # EX: Img%04d.png $prefix = $1; $format = $2; $ext = $3; printf(" \$prefix: >%s<\n", $prefix); printf(" \$format: >%s<\n", $format); printf(" \$ext: >%s<\n\n", $ext); $format =~ /%([0-9]+)d/; # Note the '%04d' portion $count = $1; $glob_str = sprintf("%s%s\.%s", # ...and build the DOS/glob match strings $prefix, "\*", $ext); $grep_str = sprintf("%s[0-9]{%d}\.%s", $prefix, $count, $ext); printf("\$glob_str: >%s<\n", $glob_str); printf("\$grep_str: >%s<\n\n", $grep_str); @tmpfiles = glob($glob_str); # Get all the glob-matched files printf("\@tmpfiles:\n"); # ...EX: Img*.png print Dumper(@tmpfiles); printf("\n"); @files = grep(/$grep_str/i, @tmpfiles); # Filter to match the 'printf format' printf("\@files:\n"); print Dumper(@files); printf("\n");