Share via


Regex 101 Exercise S5 - Strip out any non-letter, non-digit characters

An easy one for this holiday week...

S5 - Strip out any non-letter, non-digit characters

Remove any characters that are not alphanumeric from a string.

Comments

  • Anonymous
    November 21, 2005
    Regex.Replace("cookie plz.", "[^a-z0-9]", "", RegexOptions.CaseInsensitive)

  • Anonymous
    November 21, 2005
    Hmmm... are we allowed to assume seven-bit data?

  • Anonymous
    November 21, 2005
    You may assume whatever you want.

    You may, however, find out later that your assumptions were wrong...

  • Anonymous
    November 21, 2005
    The comment has been removed

  • Anonymous
    November 21, 2005
    Does .NET support POSIX classes?

    # http://www.gnu.org/software/gawk/manual/html_node/Character-Lists.html#table-char-classes
    # For example, before the POSIX standard, you had to write /[A-Za-z0-9]/ to match alphanumeric characters. If your character set had other alphabetic characters in it, this would not match them, and if your character set collated differently from ASCII, this might not even match the ASCII alphanumeric characters. With the POSIX character classes, you can write /[[:alnum:]]/ to match the alphabetic and numeric characters in your character set.

    If so...

    using System.Text.RegularExpressions;

    string AlphaNumericContentsOf(string s)
    {
    return Regex.Replace(s, "[^[:alnum:]]", "", RegexOptions.None);
    }

    Should handle Unicode data intelligently.

  • Anonymous
    November 21, 2005
    Regex.Replace( SomeStringToStrip, "W","");

    I like using the W character class. Basically the upper case version means the opposite of w.

  • Anonymous
    November 23, 2005
    Maurits,

    .NET regex doesn't support POSIX character classes, but I think it supports the unicode stuff.

    Jay,

    W is close, but not quite. If you look at the docs, you will find that it matches a bunch of unicode categories rather than specific characters, and even in ecmascript mode, it includes "_" in addition to the alphanumerics.

  • Anonymous
    June 09, 2009
    PingBack from http://quickdietsite.info/story.php?id=3665

  • Anonymous
    June 16, 2009
    PingBack from http://topalternativedating.info/story.php?id=2485