Perl string replacement

I've used string substitution in perl a couple of times and have substrings and replace them with something else.

I am curious if there is a trick to only preserve certain characters, in particular I want to remove any characters from a string that is not az, AZ, or 0-9.

eg. a b c !@#$%^&*()_~+=[]{}\|;':",./<>? 123

will beabc123

+3


source to share


1 answer


Using regex,

s/[^a-zA-Z0-9]//g;

      



using translation,

tr/a-zA-Z0-9//dc;

      

+3


source







All Articles