简单的正则表达式应该适用于此。
private Dictionary ParseCommentVariables(string contents)
{
Dictionary variables = new Dictionary();
Regex commentParser = new Regex(@"", RegexOptions.Compiled);
Regex variableParser = new Regex(@"\b(?[^:]+):\s*""(?[^""]+)""", RegexOptions.Compiled);
var comments = commentParser.Matches(contents);
foreach (Match comment in comments)
foreach (Match variable in variableParser.Matches(comment.Value))
if (!variables.ContainsKey(variable.Groups["name"].Value))
variables.Add(variable.Groups["name"].Value, variable.Groups["value"].Value);
return variables;
}
将首先提取'contents'字符串中的所有注释。然后它会提取它找到的所有变量。它将它们存储在字典中并将其返回给调用者。
即:
string contents = "some other HTML, lalalala foobarfoobarfoobar";
var variables = ParseCommentVariables(contents);
string variable1 = variables["variable1"];
string variable2 = variables["variable2"];