1

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 )

protoss
  • 117
  • 10
  • http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/ is traditional thread on matching nested pairs of tags... By clarifying what exactly you consider valid match and limiting possible nesting you will give chance to someone to provide concrete answer in your case. – Alexei Levenkov Mar 08 '16 at 02:19

1 Answers1

1

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
NoName
  • 7,940
  • 13
  • 56
  • 108