using System.Collections.Generic;
using System.Text.RegularExpressions;
public static Dictionary<string, string> data = new Dictionary<string, string>()
{"key2", "value2 - $PRP:\"key1\"" },
{"key3", "$PRP:\"key5\"$PRP:\"key6\"" },
{"key4", "$PRP:\"key1\"qwerty" },
{"key7", "$PRP:\"key6\"" },
{"key8", "$PRP:\"key3\" OY $PRP:\"key5\"$PRP:\"key7\"" },
{"key9", "$PRP:\"key1\" $PRP:\"key2\" $PRP:\"key3\" $PRP:\"key4\" $PRP:\"key5\" $PRP:\"key6\" $PRP:\"key7\" $PRP:\"key8\" $PRP:\"key9\"OMG" },
{"key10", "Foo $PRP:\"key12\" Hello" },
{"key11", "Bar $PRP:\"key10\" World" },
{"key12", "Baz $PRP:\"key11\" :)" },
public static Dictionary<string, string> answers = new Dictionary<string, string>()
{"key2", "value2 - value1" },
{"key4", "value1qwerty" },
{"key8", "ATR Soft OY ATR Soft" },
{"key9", "value1 value2 - value1 ATR Soft value1qwerty ATR Soft Soft ATR Soft OY ATR Soft OMG" },
{"key10", "Foo Baz Bar World :) Hello" },
{"key11", "Bar Foo Baz :) Hello World" },
{"key12", "Baz Bar Foo Hello World :)" },
public static void Main()
foreach(var key in data.Keys)
if (answers.ContainsKey(key))
string res = ResolveKey(key, data);
Console.WriteLine(string.Format("'{0}' -> '{1}'", key, res));
if (string.Compare(answers[key], res) != 0)
Console.WriteLine(string.Format("\nFAIL! Correct would be\n'{0}' -> '{1}'", key, answers[key]));
Console.WriteLine("Congratulations! :)");
private static string keyPattern = @"key[0-9]*";
private static string prpPattern = @"\$PRP:""";
private static string doubleQuotePattern = @"""";
private static string notationPattern = String.Concat(prpPattern, keyPattern, doubleQuotePattern);
private static string parentKey = "";
private static int depth = 0;
public static string ResolveKey(string key, Dictionary<string, string> data)
if(depth == 0) parentKey = key;
if(depth > 0 && key.Equals(parentKey)) return "";
string replacePattern = "";
if(data.ContainsKey(key)) {
MatchCollection matchCollection = MatchPointerKeys(value);
for (int count = 0; count < matchCollection.Count; count++) {
pointer = matchCollection[count].Value;
childKey = Regex.Match(pointer, keyPattern).ToString();
replacePattern = String.Concat(prpPattern, childKey, doubleQuotePattern);
childValue = ResolveKey(childKey, data);
value = Regex.Replace(value, replacePattern, childValue);
private static MatchCollection MatchPointerKeys(string value) {
Regex notation = new Regex(notationPattern);
MatchCollection matchedNotations = notation.Matches(value);