protoss
protoss

Reputation: 127

C# regex replace tokenized string

I have a string like ths:

       string s = "{{hello {{User.Name}},thanks for your buying in {{Shop}}";

And how can I use like:

       IDictionary<string,string> vals=new Dictionary<string,string>()
        {
            {"User.Name","Jim" },
            {"Shop","NewStore" }
        }
        string result= Regex.Replace(s, @"{{.+}}", m => vals[m.Groups[1].Value]);

but it doesn't because regex will match the whole string (The first two {{ actually in string,not token )

Upvotes: 1

Views: 96

Answers (1)

NoName
NoName

Reputation: 8025

I assume all keys int your vals dictionary don't contain { and } character.

To deal with this case, dont use . in the {{.+}} match. . accept any single character except \n (in case your regex code).

Replace . with [^\{\}], which match any character except { and }.
And you should escape { and } in your regex function, because they have special meaning in Regex. Some cases Regex treat them as literal charater, but not in other cases.

To have m.Groups[1], you have to wrap [^\{\}]+ inside ( and ).

Finally, to avoid exception, check if your dictionary keys contain a string found by above Regex function before replace it.

Your code can be like bellow:

string s = "{{hello {{User.Name}}, thanks for your buying in {{Shop}}. This {{Tag}} is not found";

IDictionary<string, string> vals = new Dictionary<string, string>()
{
    {"User.Name","Jim" },
    {"Shop","NewStore" }
};

string result = Regex.Replace(s, @"\{\{([^\{\}]+)\}\}",
    m => vals.ContainsKey(m.Groups[1].Value) ? vals[m.Groups[1].Value] : m.Value);
Console.WriteLine(result);

Output:

{{hello Jim, thanks for your buying in NewStore. This {{Tag}} is not found

Upvotes: 1

Related Questions