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

Below is what I have done, I would like to improve it. How would a monk do it? Thanks,
use strict; my $newvar; my $var = "123, abc; don't use (these)"; my(@var) = split /\s+/, $var; foreach (@var) { s/\W//g; $newvar .= "$_ "; } print "VAR: $newvar"; #VAR: 123 abc dont use these

Replies are listed 'Best First'.
Re: How do I remove non-word chars but leave spaces intact?
by japhy (Canon) on Mar 13, 2001 at 03:48 UTC
    You could use: tr/ \n\r\t\fa-zA-Z0-9_//cd Not locale-friendly, though. For that, use: s/[^\w\s]+//g
Re: How do I remove non-word chars but leave spaces intact?
by bjelli (Pilgrim) on Mar 13, 2001 at 19:31 UTC

    remove anything thats not either a word or whitespace

           $var =~ s/[^\w\s]//g;
    --
    Brigitte    'I never met a chocolate I didnt like'    Jellinek
    http://www.horus.com/~bjelli/         http://perlwelt.horus.at