将正则表达式处理的内容转换到Dictionary中

string content = "{'A'='1','B'='2','C'='3','D'='4','E'='5','F'='6'}";
string pattern = @"'[a-zA-Z]*?'='[\w\d\.\-\:\s\*]*'?";

//将content数据转换为字典 
Dictionary dict = RegexToDictionary(content, pattern);



//将JSON转换成字典
private Dictionary RegexToDictionary(string content, string pattern)
{
var ms = Regex.Matches(content, pattern, RegexOptions.IgnoreCase);
Dictionary dic = new Dictionary();
if (ms.Count>0)
{
foreach (Match m in ms)
{
string s = m.Value.Replace("'", "");
string[] ss = s.Split(new char[] { '=' });
dic.Add(ss[0], ss[1]);
}
}
return dic;
}

你可能感兴趣的:(C#,/,ASP.Net,正则表达式)