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);
..............

解决方案 »

  1.   

                string str = "74<50>,110<12>,23<324>,343<2>,345<432>";
                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));
                }
      

  2.   


    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]);
     
    }
      

  3.   

    谢谢哈,顺便请教下  如何获取文件名呢? 比如说string file="c:\1\3\5324.txt",获取5324.txt 怎么获取呢?
      

  4.   

    这个也不需要使用正则void Main()
    {
    Console.WriteLine(System.IO.Path.GetFileName(@"c:\1\3\5324.txt"));
     //5324.txt}
      

  5.   

    Regex.Match(@"c:\1\3\5324.txt",@"(?<=\\)[^\\]+$").Value