princepawn has asked for the wisdom of the Perl Monks concerning the following question:

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?

Replies are listed 'Best First'.
Re: regex to match a literal dot near end of string
by japhy (Canon) on Jul 18, 2001 at 20:43 UTC
    You could be a little wittier and not go all the way to the end and then backtrack:
    ($ext) = $name =~ /(\.[^.]*)+/;

    _____________________________________________________
    Jeff japhy Pinyan: Perl, regex, and perl hacker.
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Re: regex to match a literal dot near end of string
by mdillon (Priest) on Jul 18, 2001 at 20:35 UTC
    i'm not willing to speculate about your gonads, but how about this code:
    sub new_filename { my ($name,$platform) = @_; $name =~ s/(\.[^.]+)$/$platform$1/ or die "$name is an invalid patch name"; $name; }
Re: regex to match a literal dot near end of string
by adamsj (Hermit) on Jul 18, 2001 at 20:39 UTC
    Well, you run the risk of matching files like filename.. or filename.^H with your regex--probably not a big deal, eh?

    Still, I'd've done something like: /(.+)(\.\w+)$/ on the assumption that all my desired filename extensions would match \w+ .

    adamsj

    They laughed at Joan of Arc, but she went right ahead and built it. --Gracie Allen

Re: regex to match a literal dot near end of string
by scain (Curate) on Jul 18, 2001 at 20:34 UTC
    Presumably, you know that only \w characters are in the suffix that you are trying to match, so replace the second .+ with \w+. I try very hard to not use .+ because it is so easy to get bitten when things don't work the way you think they might.

    Scott

(Elbie) Re: regex to match a literal dot near end of string
by elbie (Curate) on Jul 19, 2001 at 00:03 UTC
    Depending on how thorough you want to be, if you're including the $ at the end, you might as well include the ^ at the beginning.

    Also, maybe someone can answer this for me. Is there an advantage to matching the \. as part of the second atom? That is to say, what is the advantage of

    $name =~ /^(.+)(\..+)$/ or die "$name is an invalid patch name"; "$1$platform$2";
    over
    $name =~ /^(.+)\.(.+)$/ or die "$name is an invalid patch name"; "$1$platform.$2";

    As an aside, I noticed you're looking for files ending in both .Z and .tar. Is it possible you will want to match files ending in .tar.Z (or .tar.gz) as well?

Re: regex to match a literal dot near end of string
by MZSanford (Curate) on Jul 18, 2001 at 20:35 UTC
    Close, but because there could be multiple, you need to use * for extra-greedyness :

    sub new_filename { my ($name,$platform) = @_; $name =~ /^(.*)(\..+)$/ or die "$name is an invalid patch name"; "$1.$platform$2"; }
    Thst should work, + is just not greedy enough
    OH, a sarcasm detector, that’s really useful
      No, + and * are equally greedy. The former just requires at least one character to match, while the latter does not.

      _____________________________________________________
      Jeff japhy Pinyan: Perl, regex, and perl hacker.
      s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

        <braindead> Not sure what i meant, when i think , i knew that. Sorry 'bout that </braindead>
        OH, a sarcasm detector, that’s really useful