How to replace all unwanted characters in a string using RegEx?
.net, c#, regex
Solution
`[^c]` means: everything that is not `c`. You should replace `c` with your allowed character and use that regex to replace method:
var reg = new Regex(@"[^ !""#$%&'()*+,-./0-9:;<=>?@A-Z\[\\\]^_`a-z{|}~]");
var result = reg.Replace(inputString, "Ã");
Problem
In a c# application i need to replace all unwanted characters with "Ã". Following is the allowed character array. ``` string[] wantedCharacters = new string[] { " ", "!", "\"", "#", "$", "%", "&", "\'", "(", ")", "*", "+", ",", "-", ".", "/", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":", ";", "<", "=", ">", "?", "@", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "[", "\\", "]", "^", "_", "`", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "{", "|", "}", "~" }; ``` All the characters other than this should be replaced using "Ã". I have done it with Loopin all the string characters. But it's taking too much time to execute. I looking for a regular expression to do this. Any help will be appreciated.