本帖最后由 ligengrong 于 2010-07-17 09:30:30 编辑

解决方案 »

  1.   

    (语言\d+)(?:[^>]*>){2}([^<]+)取分组1,2
      

  2.   

            private static void TestRegex08()
            {
                string test = @"<table cellpadding=""0"" cellspacing=""1"" class=""tb_1"">
      <tr><th width=""92"">语言1</th><td width=""211"">保加利亚语<td><td width=""105"">熟练<td><td width=""360"">这个语言呢,我描述一下感想。</td></tr><tr><th width=""92"">语言2</th><td width=""211"">蒙古语<td><td width=""105"">熟练<td><td width=""360"">这个蒙古语呢,强大,我来描述下</td></tr>
      </table>(?is)<tr>[^<]*<th[^>]*>语言([^>]+>){2}(?<language_name>[^<\r\n]+)([^>]*>){2}(?<language_grade>[^<\r\n]+)([^>]*>){2}(?<language_description>[^<\r\n]*)";
                MatchCollection mc = Regex.Matches(test, @"(语言\d+)(?:[^>]*>){2}([^<]+)");
                foreach (Match m in mc)
                {
                    Console.WriteLine(m.Groups[1].Value + " : " + m.Groups[2].Value);
                }
            }
      

  3.   


    大哥 可以不可以用key  就来取的!? 以为 我要所有中文信息都要取到!? 多谢你每次的帮忙!thanks
      

  4.   

    自己添加一下不就行了。都取出来了。不用key,你还容易些啊。        private static void TestRegex08()
            {
                string test = @"<table cellpadding=""0"" cellspacing=""1"" class=""tb_1"">
      <tr><th width=""92"">语言1</th><td width=""211"">保加利亚语<td><td width=""105"">熟练<td><td width=""360"">这个语言呢,我描述一下感想。</td></tr><tr><th width=""92"">语言2</th><td width=""211"">蒙古语<td><td width=""105"">熟练<td><td width=""360"">这个蒙古语呢,强大,我来描述下</td></tr>
      </table>(?is)<tr>[^<]*<th[^>]*>语言([^>]+>){2}(?<language_name>[^<\r\n]+)([^>]*>){2}(?<language_grade>[^<\r\n]+)([^>]*>){2}(?<language_description>[^<\r\n]*)";
                MatchCollection mc = Regex.Matches(test, @"(语言\d+)(?:[^>]*>){2}([^<]+)");
                Dictionary<string, string> dict = new Dictionary<string, string>();
                foreach (Match m in mc)
                {
                    dict.Add(m.Groups[1].Value, m.Groups[2].Value);                
                }
                Console.WriteLine(dict["语言1"]);
            }
      

  5.   

    大哥 我是说 在正则表达式内的?<language_name>这些! 可以做成这样的效果么!?
      

  6.   

    (?<zz>)这叫命名捕获组,如果组很多,一般会用这个,否则,就用下标就很清晰了。你这次的和之前的不同。你无法写一个很长很长的来用一次匹配成功。即便用一次匹配,找多个捕获,一样要写循环的。
      

  7.   

    你测试过我给你的没有?
    难道不是同时?
    m.Groups[1].Value 就是 语言1
    m.Groups[2].Value 就是 语言1对应的值
    不明白你是一定要给这个加个名字还是什么意思
      

  8.   

    哦, 我只是想 在html里太多这样的格式 同时我也需要每组去去出不同的内容,记录没个信息!
      

  9.   

    可以取到了!原来(?is)和(?nis)的作怪 大哥可以解释一下么!?
      

  10.   

    (?isn)
    i:忽略大小写
    s:.匹配回车换行,表示单行模式
    n:取消未命名的分组。