Here is how you can do it. You marked Perl, so I will give you a perlish solution:
my $string = q{That is a ~ v%^&*()ery co$ol ' but not 4 realistic T3st}; print $string . "\n"; $string =~ s{[^-a-zA-Z '~]}{}g; print $string . "\n";
Print
That is a ~ v%^&*()ery co$ol ' but not 4 realistic T3st That is a ~ very cool ' but not realistic Tst
To make it clear:
$string =~ s{[^-a-zA-Z '~]}{}g;
matches characters that are not [^..] inside brackets [ , ] and do not replace them with anything. The g flag at the end of a replacement replaces more than 1 character.
user1558455
source share