<a href='地址' class='BookName' title='可变'>名称</a>请问下,我想获取地址 和名称。在asp.net中应该如何写
我试了写了下在测试的工具中可以,到asp.net中就是不行

解决方案 »

  1.   

    using System;
    using System.Text;
    using System.Text.RegularExpressions;class Test
    {
        static void Main()
        {
            string strHyerLink = "<a href='地址' class='BookName' title='可变'>名称 </a> ";
            Match m = Regex.Match(strHyerLink, "href=['\"]?(.*?)['\" ].*?>(.*?)</");
            bool bl = m.Success;
            if (bl)
            {
                Console.WriteLine("OK");
                Console.WriteLine(m.Groups[1].Value);
                Console.WriteLine(m.Groups[2].Value);
            }
            Console.ReadKey();
        }
    }
      

  2.   

    再问下,如果我想取出 地址 和名称 应该如何写?下面的是我写的,不知道为什么在工具里能匹配到,在asp.net里就匹配不到
    <a\s?href='(.*)'\s?class='BookName'\s?title='(.*)'>(.*)</a>
      

  3.   

       string strHyerLink = "<a href='地址' class='BookName' title='可变'>名称 </a> ";
            Match m = Regex.Match(strHyerLink, @"<a\s?href='(.*)'\s?class='BookName'\s?title='(.*)'>(.*) </a>");
            //你这表达式虽然可捕获结构但是扩展性太差源代码稍稍改动你就得重写
        
            bool bl = m.Success;
            if (bl)
            {
                Console.WriteLine("OK");
                Console.WriteLine(m.Groups[1].Value);
                Console.WriteLine(m.Groups[2].Value);
            }
            Console.ReadKey();
      

  4.   

      string strHtml = "<a href='1038075,21072281.aspx' class='BookName' title='字数:5743 更新时间:2008-8-21 18:13:42'> 第三十三章节火神殿</a></td></tr><tr><td style='width:33%' class='chaptertd'><a href='1038075,21081291.aspx' class='BookName' title='字数:5665 更新时间:2008-8-22 19:58:34'> 第三十四章节神冢</a></td><td style='width:33%' class='chaptertd'><a href='1038075,21088439.aspx' class='BookName' title='字数:6238 更新时间:2008-8-23 19:39:46'> 第三十五章节青莲妙风</a></td><td style='width:33%' class='chaptertd'><a href='1038075,21096401.aspx' class='BookName' title='字数:4407 更新时间:2008-8-24 20:10:02'> 第三十六章节背叛</a>";
            MatchCollection mc = Regex.Matches(strHtml, @"<a.*?href=['""]?(.*?)['"" ]+.*?title=['""]?(.*?)['""]+.*?>(.*?)</");
            foreach (Match m in mc)
            {
                Response.Write(m.Groups[1].Value); Response.Write(m.Groups[2].Value);
                Response.Write(m.Groups[3].Value);
            }