I am trying to parse key/value token strings out of text using Regex in C#. For example, given the string:
stuff{token1:param1}stuff{token2}stuff{token3:param3}stuff
I would end out with three capture groups:
(key=token1,value=param1)
(key=token2,value=(null))
(key=token3,value=param3)
My problem isn't writing a regular expression to do the parsing, my problem is grouping together the keys and values.
Here's the code I have so far. ("tokenPattern" is split out just to make the regular expression a bit less overwhelming.)
var text = "{token1:param1}stuff{token2}more stuff{token3:param3}";
var tokenPattern = @"(?<token>[{](?<key>[^:}]*)([:](?<value>[^}]*))?[}])";
var pattern = @"([^{]*" + tokenPattern + ")*.*?";
var match = Regex.Match(text, pattern);
The match is successful, and the the resulting Groups of interest in the "match" variable are:
? match.Groups["token"].Captures
Count = 3
[0]: {{token1:param1}}
[1]: {{token2}}
[2]: {{token3:param3}}
? match.Groups["key"].Captures
Count = 3
[0]: {token1}
[1]: {token2}
[2]: {token3}
? match.Groups["value"].Captures
Count = 2
[0]: {param1}
[1]: {param3}
Because the "value" portion of my tokens are optional, I can't find a way to match up "key"s with "value"s. I could, I suppose, re-parse each the "token" captures but that seems a bit of a pain.
Is there a way to drill into a "token" capture to find the associated "key" and "value" (if any) for that specific capture?