Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^2: Regex To Remove File Extension

by grep (Monsignor)
on Dec 10, 2008 at 21:09 UTC ( #729507=note: print w/ replies, xml ) Need Help??


in reply to Re: Regex To Remove File Extension
in thread Regex To Remove File Extension

You're assuming too much, your regex will fail on:

index.html foo.pl CGI.pm video.mpeg foo.pl~
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 foo.pl CGI.pm video.mpeg foo.pl~ .bash_hist +ory .bash_rc /; foreach my $string ( @names ) { print "$string -> "; $string =~ s/(.+)\.[^.]+$/$1/; print "$string\n"; }
grep
One dead unjugged rabbit fish later...


Comment on Re^2: Regex To Remove File Extension
Select or Download Code
Re^3: Regex To Remove File Extension
by n3toy (Friar) on Dec 11, 2008 at 01:08 UTC
    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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (9)
As of 2014-12-18 05:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (42 votes), past polls