Share via


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

  • Anonymous
    January 17, 2006
    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
  • Anonymous
    January 17, 2006
    Oops I mean
    s = Regex.Replace(s, @"[^t -~]", ""); // note the ^
  • Anonymous
    January 17, 2006
    The comment has been removed