How to replace the characters !@#$%^*_+\= in a string using RegEx with C#

1 Answer

0 votes
using System;
using System.Text.RegularExpressions;

class ReplaceSpecialChars
{
    static void Main()
    {
        string input = "The!quick@brown#fox$jumps%^over*_the+\\lazy=dog.";
        string pattern = "[!@#$%^*_+=\\\\]"; 
        string replacement = " ";

        // Perform regex replacement
        string result = Regex.Replace(input, pattern, replacement);

        Console.WriteLine("Original: " + input);
        Console.WriteLine("Modified: " + result);
    }
}



/*
run:

Original: The!quick@brown#fox$jumps%^over*_the+\lazy=dog.
Modified: The quick brown fox jumps  over  the  lazy dog.

*/

 



answered Jun 11, 2025 by avibootz
...