fork(1) download
  1. using System;
  2. using System.Text;
  3. using System.Text.RegularExpressions;
  4. using System.Collections.Generic;
  5.  
  6. public class Test
  7. {
  8.  
  9. public static string Format(string formatWithNames, IDictionary<string, object> data) {
  10. int pos = 0;
  11. var args = new List<object>();
  12. var fmt = Regex.Replace(
  13. formatWithNames
  14. , @"(?<={)[^}]+(?=})"
  15. , new MatchEvaluator(m => {
  16. var res = (pos++).ToString();
  17. var tok = m.Groups[0].Value.Split(':');
  18. args.Add(data[tok[0]]);
  19. return tok.Length == 2 ? res+":"+tok[1] : res;
  20. })
  21. );
  22. return string.Format(fmt, args.ToArray());
  23. }
  24.  
  25. public static void Main()
  26. {
  27.  
  28.  
  29. var x = new Dictionary<string,object> {
  30. {"brown", 123}
  31. , {"jumps", 21}
  32. , {"lazy", 42}
  33. };
  34. Console.WriteLine("{0}", Format("Quick {brown} fox {jumps:C} over the {lazy:P} dog", x));
  35.  
  36. }
  37. }
Success #stdin #stdout 0.12s 24592KB
stdin
Standard input is empty
stdout
Quick 123 fox $21.00 over the 4,200.00 % dog