Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

How to extract all alphanumeric characters from a string?

by biswanath_c (Beadle)
on Nov 13, 2009 at 21:11 UTC ( #807061=perlquestion: print w/ replies, xml ) Need Help??
biswanath_c has asked for the wisdom of the Perl Monks concerning the following question:


I'd like to extract all alphanumeric characters from a given string and store them into another string. How can i achieve this?

Comment on How to extract all alphanumeric characters from a string?
Re: How to extract all alphanumeric characters from a string?
by ikegami (Pope) on Nov 13, 2009 at 21:13 UTC
    utf8::upgrade( my $alphanums = $chars ); $alphanums =~ s/[\W_]//g;
    or
    ( my $alphanums = $chars ) =~ s/\P{Alnum}//g;

    utf8::upgrade makes /\w/ match alphanums and "_", and /\W/ everything else.

    Update: The original solution didn't remove underscores. Fixed.
    Update: Added alternative method.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2014-09-01 19:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (17 votes), past polls