[Fwd: SPUG:regex question]

Michael R. Wolf MichaelRunningWolf at att.net
Tue Jan 28 14:27:08 CST 2003

Islandman <schieb at centurytel.net> writes:

> Will try again. Expected I was the only one who did not know the regex
> for removing all non-printable characters from a text string, but
> haven't got the answer just yet. 
> Super-spiffy regex to accomplish this would be most appreciated.

I tried to use a \p{IsPrint}, but didn't get it to work.  The [:print]
POSIX character works as below.

#! /usr/bin/perl -w

my $all;
$all .= $_ for map {chr($_)} 0..1024;

if (open CATV, "| cat -vet") {
    printf CATV "Everything(%d chars), printed visably\n", length($all);
    print CATV $all;
    close CATV;
} else {
    warn "can't open cat for visable printing: $!";

# Remove the non-printables.  See pg 175 of "Programming Perl" 3rd
# edition for character classes.
my $printable = $all;
$printable =~ s#[[:^print:]]##g;

print "\n" x 3, "=" x 78, "\n";
printf "Only the printables (%d chars):\n", length($printable);
print "V" x length($printable), "\n";
print $printable, "\n";
print "^" x length($printable), "\n";

More information about the spug-list mailing list