Ok, I am about to rename a bunch of files which end in dot-something, ie:
.Z .zip .tar .ar
But I must insert $platform just before the dot-something, e.g.:
solaris.Z linux_alpha.zip
So I wrote this regexp (I have plenty of testosterone --- no wimpy split and join or rindex on '.' for me):
sub new_filename { my ($name,$platform) = @_; $name =~ /(.+)(\..+)$/ or die "$name is an invalid patch name"; "$1$platform$2"; }
So I think my regex is right. I am saying: be greedy and go all the way to the end of the string, but you must give up characters until the second parenthesized expression has been filled with a literal dot and at least one character after it. and therefore the second literal expression will have the file extension in it and I just need to pop $platform in between the two matched parts of the string.

But am I really right? Or have my gonads gotten the best of me?


In reply to regex to match a literal dot near end of string by princepawn

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.