1

I'm looking for a regular expression that can remove all the following characters from a string (and whitespace too):

~ % & \ ; : " ' , < > ? #

I tried it with following code:

var MyCleanString = Regex.Replace(InputString, @"[~%&\\;:,<>?#\s]", String.Empty);

and it is not working.

Paolo Moretti
  • 54,162
  • 23
  • 101
  • 92
KentZhou
  • 24,805
  • 41
  • 134
  • 200

2 Answers2

0

The code below prints "abcdef", so I'm not sure why you would say that it doesn't work.

var InputString = "abc~%&\\;:,<>?# def";
var MyCleanString = Regex.Replace(InputString, @"[~%&\\;:,<>?#\s]", String.Empty);

Console.WriteLine(MyCleanString);
Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317
0

Try this :

public static string RemoveChar(string input)
{

Regex r = new Regex("(?:[^a-z0-9 ]|(?<=['\"])s)", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled);
    return r.Replace(input, String.Empty);
}
Ali Vojdanian
  • 2,067
  • 2
  • 31
  • 47