You can achieve this with a global substitution using regular expression look ahead. Like this

use strict; use warnings; while(<DATA>) { chomp; my $old = $_; s/\.(?=.*?\.)/_/g; print "Change: $old to $_\n"; } __END__ test0.file0.new_20060411.zip test1.file1.new_20060411.zip test2.file2.new_20060411.zip test3.file3.new_20060411.zip

produces

Change: test0.file0.new_20060411.zip to test0_file0_new_20060411.zip Change: test1.file1.new_20060411.zip to test1_file1_new_20060411.zip Change: test2.file2.new_20060411.zip to test2_file2_new_20060411.zip Change: test3.file3.new_20060411.zip to test3_file3_new_20060411.zip

The substitution globally replaces every dot with an underscore as long as that dot is followed somewhere later in the line by another dot. Thus, it will not replace the dot that precedes the extension as it is the last one.

I hope this helps.

Cheers,

JohnGG


In reply to Re: AWTDI: Renaming files using regexp by johngg
in thread AWTDI: Renaming files using regexp by nimdokk

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.