Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove a list of special characters from string in C# [duplicate]

Tags:

c#

regex

I need to replace a string with specific list of special characters to string.Empty.

Example: 123 ~Main To 123 Main

List of Special character : + - && || ! ( ) { } [ ] ^ " ~ * ? : \

I know that we can do like this below, can we have better way, using some Regular expressions.

> keyword = keyword.Replace("+", string.Empty);
>         keyword = keyword.Replace("&&", string.Empty);
>         keyword = keyword.Replace("||", string.Empty);
>         keyword = keyword.Replace("!", string.Empty);
>         keyword = keyword.Replace("(", string.Empty);
>         keyword = keyword.Replace(")", string.Empty);
>         keyword = keyword.Replace("{", string.Empty);
>         keyword = keyword.Replace("}", string.Empty);
>         keyword = keyword.Replace("[", string.Empty);
>         keyword = keyword.Replace("]", string.Empty);
>         keyword = keyword.Replace("^", string.Empty);
>         keyword = keyword.Replace("~", string.Empty);
>         keyword = keyword.Replace("*", string.Empty);
>         keyword = keyword.Replace("?", string.Empty);
>         keyword = keyword.Replace(":", string.Empty);
>         keyword = keyword.Replace("\\", string.Empty);
>         keyword = keyword.Replace("\"", string.Empty);

Thanks in advance.

like image 880
Varun Avatar asked Dec 03 '25 10:12

Varun


2 Answers

You can build regex on the fly - \+|&&|\|\||!|\(|\)|\{|}|\[|]|\^|~|\*|\?|:|\\|":

string input = "Hello world!";
string[] replaceables = new[] { "+", "&&", "||", "!", "(", ")", "{", "}", "[", "]", "^", "~", "*", "?", ":", "\\", "\"" };
string rxString = string.Join("|", replaceables.Select(s => Regex.Escape(s)));
string output = Regex.Replace(input, rxString, string.Empty);

You also can optimize regex as @Robin suggested (though the generation becomes complicated) - [\+!\(\)\{}\[\]\^~\*\?:\\"]|&&|\|\|

string rxString = string.Join("|", replaceables.GroupBy(r => r.Length > 1)
                                               .Select(g => g.Key ? string.Join("|", g.Select(r => Regex.Escape(r)))
                                                                  : string.Format("[{0}]", string.Join(string.Empty, g.Select(r => r == "]" ? "\\]" : Regex.Escape(r))))));

Slightly more clean regex (only metacharacters between square brackets are escaped) - [+!(){}[\]\^~*?:\\"]|&&|\|\|.

string rxString = string.Join("|", replaceables.GroupBy(r => r.Length > 1)
                                               .Select(g => g.Key ? string.Join("|", g.Select(r => Regex.Escape(r)))
                                                                  : string.Format("[{0}]", string.Join(string.Empty, g.Select(r => new[] { "]", @"\", "-", "^" }.Contains(r) ? @"\" + r : r)))));

The cleanest possible regex (additional validation for requirements to escape metacharacters) - [+!(){}[\]^~*?:\\"]|&&|\|\|

string rxString = string.Join("|", replaceables.GroupBy(r => r.Length > 1)
                                               .Select(g => g.Key ? string.Join("|", g.Select(r => Regex.Escape(r)))
                                                                  : string.Format("[{0}]", string.Join(string.Empty, g.Select((r, i) => r == @"\" || r == "]" && g.Count() > 1 || r == "^" && i == 0 || r == "-" && i > 0 ? @"\" + r : r)))));

If you have strings like Hello &|&&|& complicated world!, then you need to pass it several times.

string output = input;
string outputRef = output;
do
{
    outputRef = output;
    output = Regex.Replace(output, rxString, string.Empty);
} while (output != outputRef);
Console.WriteLine(output); // Hello  complicated world
like image 107
Ulugbek Umirov Avatar answered Dec 06 '25 00:12

Ulugbek Umirov


try this...

        //to remove non alphanumeric characters (special characters) from a string?
        public static string removespclchr(string input)
        {
            Regex regex = new Regex("[^a-zA-Z0-9]");
            return regex.Replace(input, "");
        }

or

string q = Regex.Replace(query, @""|['"",&?%\.*:#/\\-]", " ").Trim();

or

string replacestr= Regex.Replace(str, "[^a-zA-Z0-9_]+", " ");
like image 34
Sunil Devre Avatar answered Dec 05 '25 23:12

Sunil Devre



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!