Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: regex to remove all non a-z and spaces

by graff (Chancellor)
on May 16, 2005 at 04:07 UTC ( #457355=note: print w/replies, xml ) Need Help??


in reply to regex to remove all non a-z and spaces

Yet another way, using the "tr" operator instead of "s///" (assuming you meant to retain all digits, not just 8 and 9).
tr/0-9A-Za-z \t\n\r//cd;
It's a little more tedious than s///, because you can't use handy shortcuts like "\s" as a cover term for all whitespace, or "\d" for all digits, but there's a good chance that if speed is an issue, it would go faster than s///.

The "c" at the end means "apply replacements to the complement of characters specified on the left side", and "d" means "delete characters for which there is no replacement character on the right side". Since there are no replacement characters at all, then everything that is not a letter, digit or whitespace will be deleted.

(You did say you wanted to retain only letters, digits and spaces, so maybe you really don't want "\t\n\r" in the expression.)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://457355]
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: (6)
As of 2020-02-25 18:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?










    Results (113 votes). Check out past polls.

    Notices?