Hi, again, folks...

I'm trying to work-out a simple/reliable regex to deal with printf 'format' strings with shell globs.

I've experimented a lot and had a look on-line... I've gone though the O'Reilly books (Perl & Regular Expression books, cookbooks, etc) and although they mention glob2pat sub (Perl Cookbook, Sect 6.9), that converts the wrong way (but see Bug in glob2pat?)... and I can't work out an 'elegant' regex to do the job I need to do.

Although... the following works Ok... but it appears pretty long-winded and ugly to me... and I'm guessing there must be a cleaner way to do it...

use Data::Dumper; ($match) = (@ARGV); # Get the 'printf format' s +tring to match printf(" \$match: >%s<\n\n", $match); # Input as `Img%04d.png` (b +ackprime 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 'prin +tf format' printf("\@files:\n"); print Dumper(@files); printf("\n");

In this case, I'm looking at running ActiveState Perl 5.16.3 under Win32, although I'll probably also need it to work under a similar Perl version on Linux as well.

I'd greatly appreciate any guidance, please.

Thanks a heap.


In reply to 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.