Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: Regex To Remove File Extension

by n3toy (Friar)
on Dec 10, 2008 at 20:04 UTC ( #729493=note: print w/ replies, xml ) Need Help??

in reply to Regex To Remove File Extension

You have plenty of good examples to chose from. This might work for you also:

my $string = ''; $string =~ s/\.\w{3}$//;

Comment on Re: Regex To Remove File Extension
Download Code
Replies are listed 'Best First'.
Re^2: Regex To Remove File Extension
by grep (Monsignor) on Dec 10, 2008 at 21:09 UTC
    You're assuming too much, your regex will fail on:
    index.html video.mpeg
    and you'll get a bad result with *nix dotfiles
    .foo .bar

    Focus on the requirements - 1) A file must contain an extension 2) the extension is everything following the final dot

    my @names = qw/ index.html video.mpeg .bash_hist +ory .bash_rc /; foreach my $string ( @names ) { print "$string -> "; $string =~ s/(.+)\.[^.]+$/$1/; print "$string\n"; }
    One dead unjugged rabbit fish later...
      Yours is a much better solution than my simple one.

      Although my solution works for the example given, it does assume that all filenames are of the format provided in the original post and does not take into consideration the examples you provided.

      Thanks for the feedback and great sample code!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://729493]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (10)
As of 2015-11-25 15:10 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (680 votes), past polls