Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

How do I remove non-word chars but leave spaces intact?

by Anonymous Monk
on Mar 13, 2001 at 03:35 UTC ( [id://64017]=perlquestion: print w/replies, xml ) Need Help??

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://64017]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2024-04-19 08:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found