Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

How to extract all alphanumeric characters from a string?

by biswanath_c (Beadle)
on Nov 13, 2009 at 21:11 UTC ( [id://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?

Replies are listed 'Best First'.
Re: How to extract all alphanumeric characters from a string?
by ikegami (Patriarch) 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
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-04-23 09:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found