Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: Regex to convert non-printable to printable char

by GlitchMr (Sexton)
on Aug 22, 2012 at 10:04 UTC ( #989002=note: print w/replies, xml ) Need Help??

in reply to Regex to convert non-printable to printable char

Just a note, \x7F is also unprintable character, In one of my scripts, I use following translation to filter non-printable characters.

tr[\0-\x1F\x7F] [\x{2400}-\x{241F}\x{2421}]

\x{2420} is space, in case you have noticed a gap in range. If you don't want to replace them with Unicode non-printable graphics characters, you could replace \x{2400}-\x{241F}\x{2421} with ? or \x{FFFD} (Unicode replacement character).

Also, in most cases you wouldn't want to match \x09 (tab), \x0A (line feed) or \x0D (carriage return). You could use this translation when you don't want to match them.

tr[\0-\x08\x0B\x0C\x0E-\x1F\x7F] [\x{2400}-\x{2408}\x{240B}\x{240C}\x{240E}-\x{241F}\x{2421}]

It also doesn't work on EBCDIC, if you want to match EBCDIC, most likely you will need different range of characters (\x00-\x3F\xFF).

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://989002]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2021-03-02 12:22 GMT
Find Nodes?
    Voting Booth?
    My favorite kind of desktop background is:

    Results (46 votes). Check out past polls.