0

I have a certain string, and I want to replace certain signs with same safe signs. So i decided to use regex for it, but I am doing something wrong.

Program looks like:

class Program
{
    static void Main(string[] args)
    {
        var bannedSignsAndReplacements = new Dictionary<string, string>
        {
            {"@", "A"},
            {"£", "L"},
            {"_", "-"},
            {"$", "S"},
            {"~", "-"},
            {"[", "("},
            {"]", ")"},
            {"{", "("},
            {"}", ")"},
            {"|", "-"},
            {"€", "E"},
            {"^", "-"}
        };

        var someString = "@${} t€st";
        var bannedSigns = string.Join("", bannedSignsAndReplacements.Select(sg => sg.Key));

        var curedString = 
            Regex.Replace(someString, $@"[{bannedSigns}]", delegate (Match match)
            {
                string val = match.ToString();
                return bannedSignsAndReplacements[val];
            });

        Console.WriteLine(curedString);
    }
}

So as you see I am having collection of banned signs and replacements for them, I am putting it into this reggex expression and I have 2 scenarios:

  • when i use @${} t€st this string I regex trying to replace ${} I think it treats it like a single char
  • when I remove ${} it ignores any pattern

So probably my pattern is wrong but Have no idea how to bite this problem. Any Ideas?

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Wojciech Szabowicz
  • 3,646
  • 5
  • 43
  • 87

0 Answers0