I’m trying to replace all characters except letters, numbers and some special characters with whitespace. This is the line of code I’m using:
documentText = Regex.Replace(documentText, @"^((?![a-zA-Z0-9%\-\@\$&']).*)$", " ");
It doesn’t work. I tested it on a sample text like this:
[]\^|+*(){} ~#%=/<>-!@$&_'",.?;:
this should stay
and it removes everything.
Use the following regex:
Using
^inverts the character class, which is perfect for what you’re looking for without using a negative lookahead.