http://qs321.pair.com?node_id=338459


in reply to leaving only [a-zA-Z0-9]

The other responses here give good solutions. But here's what went wrong with your approach.

You said /!([a-zA-Z0-9])/ in the regex part of your substitution, by which you meant, "match anything that is not a letter or number". But what you wrote actually means "match an exclamation point followed by any letter or number". Clearly not what you wanted.

The way to say what you wanted that is closest to what you wrote is, /([^a-zA-Z0-9])/. The caret as the first character in the character class does what you wanted the excalmation point to do. And the parentheses do nothing for you in the current instance so you can leave them out.

The tr/// example was recommended to you because, for tasks like this, it is faster, conceptually cleaner, and more idiomatic.

------------------------------------------------------------
"Perl is a mess and that's good because the
problem space is also a mess.
" - Larry Wall