Home > Regular Expressions > Regular Expressions: Remove all special characters from a string, allowing only alphanumeric and chars: ‘.’ and ‘-’

Regular Expressions: Remove all special characters from a string, allowing only alphanumeric and chars: ‘.’ and ‘-’

20:28

Marek Śliwiński Leave a comment Print This Post  (2,898) Go to comments

Remove all special characters from a string, allowing only alphanumeric and chars: ‘.’ and ‘-’

Example:

string source = ".ąśę0123^%($&Marek&*(@&@#-";
string result
  = System.Text.RegularExpressions.Regex.Replace(source, @"[^\w\.-]", "");
// result= ".ąśę0123Marek-"
Share and Enjoy:
  • DotNetKicks
  • Digg
  • del.icio.us
  • Wikio IT
  • Google Bookmarks
  • Facebook
  • Print
  1. Deckard Cain
    Deckard Cain
    November 3rd, 2009   (Quote) at 09:56   (Quote) | #1

    Worked great in java, thanks!!

    str = str.replaceAll(“[^\\w\\.-]“, “”);

  1. No trackbacks yet.

Subscribe without commenting