Remove Non Printable Characters From String Using Ruby Regex

non printable characters
non printable characters

I Recently was working on building some Selenium tests and needed to compare a string after being saved to a database and returned to the user interface. Because of the interface plugin that does some syntax highlighting, the string was being displayed with some non printable characters. Because the non printable characters were not breaking the app, I decided that it would be OK for me to remove them to compare the strings in my selenium test.

To remove the non printable characters or only return the printable characters I used the following ruby:

printableString = stringFromInterface.scan(/[[:print:]]/).join

There is a great list of other character classes that can be used in your regular expressions found at

Character classes

Class Switch Match description
[0-9] \d Decimal digit character
[^0-9] \D Not a decimal digit character
[\s\t\r\n\f] \s Whitespace character
[^\s\t\r\n\f] \S Not a whitespace character
[A-Za-z0-9_] \w Word character (alpha, numeric, and underscore)
[^A-Za-z0-9_] \W Not a word character
[:alnum:] Alpha numeric ([A-Za-z0-9])
[:alpha:] Uppercase and lowercase letters ([A-Za-z])
[:blank:] Blank or tab character
[:space:] Whitespace characters
[:digit:] Decimal digit characters
[:lower:] Lowercase letters ([a-z])
[:upper:] Uppercase characters
[:print:] Any printable character, including space
[:graph:] Printable characters excluding space
[:punct:] Punctuation characters: any printable character excluding aplhanumeric or space
[:cntrl] Chontrol characters (0x00 to 0x1F and 0x7F)
[:xdigit:] Hexadecimal digits ([0-9a-fA-F])

Published by


I am a Geek! I hope you enjoyed this recent post.

3 thoughts on “Remove Non Printable Characters From String Using Ruby Regex”

Comments are closed.