Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
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 cooling their heels in the Monastery: (16)
As of 2015-07-02 21:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (45 votes), past polls