Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

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

by graff (Chancellor)
on May 16, 2005 at 04:07 UTC ( [id://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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://457355]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (2)
As of 2024-04-24 17:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found