Use Search :
Regex template = new Regex(@"\{(?<key>.+?)\}(?<value>[^{}]*)");
ILookup<string, string> dictionary = template.Matches(MyString)
.Cast<Match>()
.ToLookup(x => x.Groups["key"].Value, x => x.Groups["value"].Value);
EDIT: If you expect to receive a "simple" set of results (eg {key1, value1}
, {key1, value2}
, {key2, value2}
instead {key1, {value1, value2} }, {key2, {value2} }
), you can get the type of results IEnumerable<KeyValuePair<string, string>>
:
Regex template = new Regex(@"\{(?<key>.+?)\}(?<value>[^{}]*)");
ILookup<string, string> dictionary = template.Matches(MyString)
.Cast<Match>()
.Select(x =>
new KeyValuePair<string, string>(
x.Groups["key"].Value,
x.Groups["value"].Value
)
);
source
share