tags:

views:

108

answers:

2

How do I remove all punctuation except for spaces from a string in Perl?

+5  A: 

Spaces aren't punctuation, and you aren't specific about whether you want to keep just spaces or all kinds of whitespace, but this substitution will remove all types of punctuation (since there are more forms of punctuation than just ! , and .).

$string =~ s/[[:punct:]]//g;

Ether
And there are more letters than a-zA-Z.
strager
There are a lot more "letters" than just A to Z. That has nothing to do with the right answer though.
brian d foy
No need for the "i" modifier. I've never heard of upper/lower cased punctuation.
runrig
@runrig: yep, that was a holdover from my first answer which had `[^a-z]`.
Ether
+9  A: 
s/[[:punct:]]//g
ghostdog74