G'day ozboomer,

"... but it appears pretty long-winded and ugly to me ..."

Agreed. I think you've thrown far too much code into that solution. In your first regex, you have character classes inside character classes ([\w]); and a pointless 'i' modifier (aim to avoid that anyway as it slows down the regex). You seem to have gone somewhat overboard with sprintf usage to create a regex; qr// would have been a better choice in my opinion (I've used it in the code below).

Here's the guts of what I think you need:

my @parts = $ARGV[0] =~ /^(\w+)%(\d+)d[.](\w+)$/; $parts[1] =~ s/^0*//; my $re = qr{(?x: ^ $parts[0] \d{$parts[1]} [.] $parts[2] $ )}; my $glob_str = "$parts[0]*.$parts[2]"; print for grep { /$re/ } glob $glob_str;

It would have been useful if you'd put together sample data, test input and actual/expected output. I dummied up these filenames for testing:

$ ls -1 Img* Img.png Img.svg Img0000.png Img1.png Img12.png Img123.png Img1234.png Img12345.png Img1239.png

I put those five lines of code into a script (pm_1195222_fmt_glob_re.pl) so that you can see each stage (much as your OP code does).

#!/usr/bin/perl -l use strict; use warnings; print 'Command line arg:'; print $ARGV[0]; my @parts = $ARGV[0] =~ /^(\w+)%(\d+)d[.](\w+)$/; print "Parts: @parts"; $parts[1] =~ s/^0*//; print "Parts (after stripping zeros): @parts"; my $re = qr{(?x: ^ $parts[0] \d{$parts[1]} [.] $parts[2] $ )}; print "Filter RE: $re"; my $glob_str = "$parts[0]*.$parts[2]"; print "Glob string: $glob_str"; print 'Found files:'; print for grep { /$re/ } glob $glob_str;

Here's a couple of sample runs (the first using your "Img%04d.png" string).

$ pm_1195222_fmt_glob_re.pl 'Img%04d.png' Command line arg: Img%04d.png Parts: Img 04 png Parts (after stripping zeros): Img 4 png Filter RE: (?^:(?x: ^ Img \d{4} [.] png $ )) Glob string: Img*.png Found files: Img0000.png Img1234.png Img1239.png
$ pm_1195222_fmt_glob_re.pl 'Img%03d.png' Command line arg: Img%03d.png Parts: Img 03 png Parts (after stripping zeros): Img 3 png Filter RE: (?^:(?x: ^ Img \d{3} [.] png $ )) Glob string: Img*.png Found files: Img123.png

You might also be interested in Win32::Autoglob.

— Ken


In reply to Re: How can I use printf FORMAT strings for (Win32) shell globs? by kcott
in thread How can I use printf FORMAT strings for (Win32) shell globs? by ozboomer

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.