using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;
public class Test
{
public static string Format(string formatWithNames, IDictionary<string, object> data) {
int pos = 0;
var args = new List<object>();
var fmt = Regex.Replace(
formatWithNames
, @"(?<={)[^}]+(?=})"
, new MatchEvaluator(m => {
var res = (pos++).ToString();
var tok = m.Groups[0].Value.Split(':');
args.Add(data[tok[0]]);
return tok.Length == 2 ? res+":"+tok[1] : res;
})
);
return string.Format(fmt, args.ToArray());
}
public static void Main()
{
var x = new Dictionary<string,object> {
{"brown", 123}
, {"jumps", 21}
, {"lazy", 42}
};
Console.WriteLine("{0}", Format("Quick {brown} fox {jumps:C} over the {lazy:P} dog", x));
}
}
dXNpbmcgU3lzdGVtOwp1c2luZyBTeXN0ZW0uVGV4dDsKdXNpbmcgU3lzdGVtLlRleHQuUmVndWxhckV4cHJlc3Npb25zOwp1c2luZyBTeXN0ZW0uQ29sbGVjdGlvbnMuR2VuZXJpYzsKCnB1YmxpYyBjbGFzcyBUZXN0CnsKCQpwdWJsaWMgc3RhdGljIHN0cmluZyBGb3JtYXQoc3RyaW5nIGZvcm1hdFdpdGhOYW1lcywgSURpY3Rpb25hcnk8c3RyaW5nLCBvYmplY3Q+IGRhdGEpIHsKCWludCBwb3MgPSAwOwogICAgdmFyIGFyZ3MgPSBuZXcgTGlzdDxvYmplY3Q+KCk7Cgl2YXIgZm10ID0gUmVnZXguUmVwbGFjZSgKCQlmb3JtYXRXaXRoTmFtZXMKCSwgICBAIig/PD17KVtefV0rKD89fSkiCgksICAgbmV3IE1hdGNoRXZhbHVhdG9yKG0gPT4gewogICAgICAgICAgICB2YXIgcmVzID0gKHBvcysrKS5Ub1N0cmluZygpOwogICAgICAgICAgICB2YXIgdG9rID0gbS5Hcm91cHNbMF0uVmFsdWUuU3BsaXQoJzonKTsKICAgICAgICAgICAgYXJncy5BZGQoZGF0YVt0b2tbMF1dKTsKICAgICAgICAgICAgcmV0dXJuIHRvay5MZW5ndGggPT0gMiA/IHJlcysiO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