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

This is probably simple for a regx expert but its somthing which has bugged me for a while. Can I strip the .pm and the path from $0 in one regx. I've always used two stages up until now.

For example from c:\root\project\myprogram.pm I would like to get myprogram.

Not earth shattering but my simple mind just can't see it. Thanks in advance.

Replies are listed 'Best First'.
Re: Another Regular Expression Question
by tinita (Parson) on Apr 08, 2004 at 15:03 UTC
    this has been done before, so instead of showing you a regular expression i'll show you this:
    use File::Basename; my ($name,$path,$suffix) = fileparse($path, qr/\..*/);
    this is easier and more portable than a regex.
    (update: ok, the suffixlist in fileparse indeed is a regex, but no need to care about slashes or other delimiters in the path)
      Thank you for your help I will start using right away.
Re: Another Regular Expression Question
by tilly (Archbishop) on Apr 08, 2004 at 15:01 UTC
    $text =~ s/.*?(\w+)\.pm/$1/i; Though I'd be more inclined to match and assign $1 somewhere rather than modify in place.
      Should be [^\\] instead of \w.

      The PerlMonk tr/// Advocate
        Depends on your assumptions. Might be better to do something like [^\\/:]. (Most people can leave out : though.)

        I should have been clearer about that.

      Thank you, I had seen this before but it had gone from my fried mind. Thanks again.
Re: Another Regular Expression Question
by pelagic (Priest) on Apr 08, 2004 at 15:05 UTC
    Not with a RE but as 1-liner anyway:
    my $program = (split /\./, ( split /[\\\/]/, $0)[-1] )[-2];

    pelagic