in reply to Changing file names as they are uploaded

Invert the character class.

$imagename =~ s/[^\w\.]/_/g;
--
<http://www.dave.org.uk>

"The first rule of Perl club is you do not talk about Perl club."
-- Chip Salzenberg

Replies are listed 'Best First'.
Re: Re: Changing file names as they are uploaded
by fireartist (Chaplain) on Sep 19, 2002 at 11:43 UTC
    but this leaves all periods in the filename, whereas I think katgirl wanted only the 'extension' period left alone.
    What's the best way to take out the non-alphanumeric symbols from the file name, but leave the .jpg extension intact?
Re: Re: Changing file names as they are uploaded
by katgirl (Hermit) on Sep 19, 2002 at 11:41 UTC
    wouldn't that still leave me with "J.R_ewing.jpg"?
      How about:
      my $rindex = rindex($imagename,'.'); $rindex = length($imagename) if $rindex == -1; substr($imagename,0,$rindex) =~ s/\W/_/g;
      It will exclude the extension from the substitution.

      Update: Fixed bug for filenames w/o extensions that end in a nonword char.

      Update2: Thought I'd golf this one a bit:

      substr($file,0,rindex($file,'.')%length"a$file") =~ s/\W/_/g;
      works correctly for $files such as "me~swimming.jpg", "J.R,ewing.jpg" and "abc;;;"

      -Blake