<a onMouseDown="return inpmv(1465);" href="http://www.docin.com/tag/%E5%84%BF%E6%AD%8C" target="_blank">儿歌<span>(4203)</span></a>  <a onMouseDown="return inpmv(1465);" href="http://www.docin.com/tag/%E6%95%B0%E5%AD%97" target="_blank">数字<span>(45833)</span></a>  一网页中包含上面的一些代码,同时也有其他的一些代码,现在要用正则把网页中的了类似上面的代码中的汉字提取出来,不要网页中的其他的汉字,该怎样提?还有,写在正则测试工具中的正则表达式和写在程序中的正则表达式不一样吗,为什么有时用测试工具可以提取出,但放到程序中就提取不出来了?

解决方案 »

  1.   

     foreach(Match m in Regex.Matches(html,@"(?i)<a[^>]*?onMouseDown=(['""\s]?)return inpmv\(\d+\);\1[^>]*?>([\u4e00-\u9fa5]+)<span>"))
        {
          Console.WriteLine(m.Groups[2].Value);
        }
    第2个问题,在程序中之所以不能用,是因为有些字符要转义  比如 "
      

  2.   

    当成xml文档去读 找到对应的节点值筛选你想要的节点
      

  3.   


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.IO;
    namespace sxLdfang
    {
        class Program
        {
            static void Main(string[] args)
            {
                string html = @"<a onMouseDown=""return inpmv(1465);"" href=""http://www.docin.com/tag/%E5%84%BF%E6%AD%8C"" target=""_blank"">儿歌<span>(4203)</span></a>   <a onMouseDown=""return inpmv(1465);"" href=""http://www.docin.com/tag/%E6%95%B0%E5%AD%97"" target=""_blank"">数字<span>(45833)</span></a>  ";
                string pattern = @"(?<=blank"">)[\u4e00-\u9fa5]+(?=<span>\(\d+\)<)";
                MatchCollection mc = Regex.Matches(html, pattern);
                foreach (Match m in mc)
                {
                    Console.WriteLine(m.Value);
                }
                Console.ReadKey();
            }
        }
    }
    运行结果:
    儿歌
    数字