Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace all unwanted characters in a string using RegEx?

Tags:

c#

.net

regex

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.

like image 259
Shiju Shaji Avatar asked Nov 24 '25 14:11

Shiju Shaji


2 Answers

[^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, "Ã");
like image 55
MarcinJuraszek Avatar answered Nov 27 '25 04:11

MarcinJuraszek


I would not use RegEx, it will be less readable.

string input "..";
HashSet<char> wantedCharactersSet = new HashSet<char>(wantedCharacters);
for (int i = 0; i < input.Length; i++)
{
    if (!wantedCharactersSet.Contains(input[i]))
        input[i] = placeholderChar;
}

Notice that HashSet<T>.Contains() has performance O(1) while Array just n.

like image 42
abatishchev Avatar answered Nov 27 '25 04:11

abatishchev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!