将正则表达式处理的内容转换到Dictionary<key, value>中
2012-06-21 20:26
661 查看
string content = "{'A'='1','B'='2','C'='3','D'='4','E'='5','F'='6'}";
string pattern = @"'[a-zA-Z]*?'='[\w\d\.\-\:\s\*]*'?";
//将content数据转换为字典
Dictionary<string, string> dict
= RegexToDictionary(content, pattern);
//将JSON转换成字典
private Dictionary<string, string> RegexToDictionary(string content, string pattern)
{
var ms = Regex.Matches(content, pattern, RegexOptions.IgnoreCase);
Dictionary<string, string> dic = new Dictionary<string, string>();
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;
}
string pattern = @"'[a-zA-Z]*?'='[\w\d\.\-\:\s\*]*'?";
//将content数据转换为字典
Dictionary<string, string> dict
= RegexToDictionary(content, pattern);
//将JSON转换成字典
private Dictionary<string, string> RegexToDictionary(string content, string pattern)
{
var ms = Regex.Matches(content, pattern, RegexOptions.IgnoreCase);
Dictionary<string, string> dic = new Dictionary<string, string>();
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;
}
相关文章推荐
- 使用java 正则表达式 只替换排除< >之内的内容
- linq学习笔记:将List<T> 转换为 Dictionary<T Key,T Value>
- Dictionary、List<KeyValuePair<i>>的组合使用
- C# Dictionary的用法 KeyValuePair<TKey, TValue> 结构
- Mvvm combobox绑定Dictionary<Key,Value>问题
- C# - OrderedDictionary<TKey, TValue> Implementation
- c# 扩展方法奇思妙用基础篇五:Dictionary<TKey, TValue> 扩展
- 正则表达式获取字符串中html<input>标签指定value值
- 控制台app.config 的 <add key="aspnet:MaxHttpCollectionKeys" value="10000" />无效
- HashTable与Dictionary<TKey, TValue>
- 都说ConcurrentDictionary<TKey, TValue>有陷阱
- SortedDictionary<TKey, TValue> 类 表示根据键进行排序的键/值对的集合。
- 正则表达式过滤 成对标签(<key>a</key> 过滤html标签)
- c# 扩展方法奇思妙用基础:Dictionary<TKey, TValue> 扩展
- HTML <a> 标签的正则表达式
- ConcurrentDictionary<TKey, TValue>的AddOrUpdate方法
- Hadoop Writable深度复制及读取任意<key,value>序列文件
- c#扩展方法奇思妙用基础篇五:Dictionary<TKey, TValue> 扩展
- Dictionary<TKey, TValue> 进行where的枚举遍历
- 怎么用正则表达式获取文章内容中的&lt;img标签和 src的路径