74<50>,110<12>,23<324>,343<2>,345<432>如何用正则解析这字符串 74为键,50为值放进Dictionary
Dictionary<int, int> dic = new Dictionary<int, int>();
dic.Add(74, 50);
dic.Add(110, 12);
..............
Dictionary<int, int> dic = new Dictionary<int, int>();
dic.Add(74, 50);
dic.Add(110, 12);
..............
string pstr = @"(\d+)<(\d+)>";
Dictionary<int, int> dic = new Dictionary<int,int>();
MatchCollection mc = Regex.Matches(str, pstr);
for(int i = 0;i< mc.Count;i++)
{
dic.Add(Convert.ToInt32(mc[i].Groups[1].Value), Convert.ToInt32(mc[i].Groups[2].Value));
}
void Main()
{
string str="74<50>,110<12>,23<324>,343<2>,345<432>";
var query=(from s1 in str.Split(',')
let s2=s1.Split(new char[]{'<','>'})
select s2).ToDictionary(s=>s[0]);
}
{
Console.WriteLine(System.IO.Path.GetFileName(@"c:\1\3\5324.txt"));
//5324.txt}