[ale] Stripping out crap characters in perl

Stephen Cristol stephen at bee.net
Wed Sep 7 15:54:50 EDT 2005


On Sep 7, 2005, at 1:50 PM, <Keith.Watson at gtri.gatech.edu> wrote:
> $buffer =~ s/[^\040-\176\12\15]//g;

or

> $buffer =~ s/[\000-\011\013\014\016-\037\177-\377]//g;

Just to be different, you could use "tr":

   $buffer =~ tr/[ -~\n\r]//cd;

The "c" complements the search list and the "d" says to delete matched 
characters without a replacement listed (there are no replacements in 
this example). Unless $buffer is "large," it won't matter, but using 
tr/// is faster than s/// (see "Programming Perl", 2nd ed., p. 541).

S

-- 
Stephen Cristol
cristol at emory.edu




More information about the Ale mailing list