本帖最后由 sky1314 于 2013-01-23 17:21:16 编辑

解决方案 »

  1.   

    int[] rooms = new int[4];
    List<string> maps = new List<string>(new string[] { "室", "厅", "厨", "卫" });
    string str = "2室2厅1卫";
    MatchCollection mc = Regex.Matches(str, "(\\d+)(\\w)");
    foreach (Match m in mc)
    {
    string key = m.Groups[2].Value;
    int idx = maps.IndexOf(key);
    if (idx > -1)
    {
    rooms[idx] = int.Parse(m.Groups[1].Value);
    }
    }
    for (int i = 0; i < maps.Count; i++)
    {
    Console.WriteLine(rooms[i] + maps[i]);
    }
    貌似也没偷到什么懒~~~
      

  2.   

       string str = "2室1厨1卫";
                var s = Regex.Matches(str, @"(?is)(?=.*?(?<room>\d+)室)?(?=.*?(?<hall>\d+)厅)?(?=.*?(?<kitchen>\d+)厨)?(?=.*?(?<toilet>\d+)卫)?.+").OfType<Match>().Select(t => new
                {
                    room = t.Groups["room"].Value,
                    hall = t.Groups["hall"].Value,
                    kitchen = t.Groups["kitchen"].Value,
                    toilet = t.Groups["toilet"].Value
                }).FirstOrDefault();
      

  3.   

    string str = "2室2厅1厨1卫";
                    Match mc = Regex.Match(str, @"((?<room>\d*?)室|(?<hall>\d*?)厅|(?<kitchen>\d*?)厨|(?<toilet>\d*?)卫)*");
                    var result = new {
                        room = mc.Groups["room"].Value,
                        hall = mc.Groups["hall"].Value,
                        kitchen = mc.Groups["kitchen"].Value,
                        toilet = mc.Groups["toilet"].Value
                    };
                    /*
                     + result { room = "2", hall = "2", kitchen = "1", toilet = "1" } <Anonymous Type>
                     */