You appear to be bogged down matching all sorts of things that you're not interested in. You only want to match the "00001" in "XYZ12345_X05_20110805_9999999_00001.TXT". This regex does that:

/(?<=_)00001(?=[.]\w+$)/

Then, you want to replace that with a random number. Your sprintf format indicates: seven digits, right-aligned, zero-padded. As the "%d" in the sprintf format already indicates an integer, you don't need to use int. Assuming zero is not a valid random number for your requirements, the range is "0000001" to "9999999". You can get that range with:

rand(9999999)+1

So, putting all that together, we get:

s/(?<=_)00001(?=[.]\w+$)/sprintf "%07d" => rand(9999999)+1/e

[Note: your "int( rand(100000)) + 999999" will produce this range: "0999999" to "1099998". I'm guessing that's not really what you want; however, if you want some different from the range I suggested, see rand for the values it returns and adjust accordingly.]

I ran some tests (which included edge cases) with this code:

$ perl -Mstrict -Mwarnings -le ' my @test_filenames = qw{ XYZ12345_X05_20110805_9999999_00000.TXT XYZ12345_X05_20110805_9999999_00001.TXT XYZ12345_X05_20110805_9999999_00002.TXT XYZ12345_X05_20110805_9999999_00001x.TXT XYZ12345_X05_20110805_00001_00001.TXT XYZ12345_X05_20110805_00001_00001xxx.TXT }; for (@test_filenames) { s/(?<=_)00001(?=[.]\w+$)/sprintf "%07d" => rand(9999999)+1/e; print; } '

Here's one sample output:

XYZ12345_X05_20110805_9999999_00000.TXT XYZ12345_X05_20110805_9999999_1656320.TXT XYZ12345_X05_20110805_9999999_00002.TXT XYZ12345_X05_20110805_9999999_00001x.TXT XYZ12345_X05_20110805_00001_0901076.TXT XYZ12345_X05_20110805_00001_00001xxx.TXT

-- Ken


In reply to Re: Filename change with regular expression by kcott
in thread Filename change with regular expression by Anonymous Monk

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.