Regex 101 Exercise I4 – remove unprintable characters from a string


Exercise I4 – remove unprintable characters from a string


Given an input string, remove all characters that are not printable.

Comments (3)

  1. Maurits says:

    s = Regex.Replace(s, @"[p{IsC}]", "");

    comes to mind.

    (IsC is the Unicode property for control characters… i.e., unprintable characters)

    For ASCII data this is equivalent to

    s = Regex.Replace(s, @"[t -~]", "")

    since the ASCII control characters are 0x00-0x08, 0x10-0x1f, and 0x7f; t = 0x09, " " = 0x20, and ~ = 0x7e

  2. Maurits says:

    Oops I mean

    s = Regex.Replace(s, @"[^t -~]", ""); // note the ^

  3. Maurits says:

    Strangely enough t doesn’t count as printable either… so that simplifies the ASCII case to

    s = Regex.Replace(s, @"[^ -~]", "");

    Puzzling. Why is "space" printable, but not "horizontal tab" or "vertical tab" or "carriage return" or "line feed"?