当前位置:编程学习 > C#/ASP.NET >>

求高手,C#正则表达式问题

<tr>
     <td height="23" align="left" class="td_hui"><table width="100%" border="0" cellspacing="0" cellpadding="0">
      
       <tr>
         <td width="85%">·
         
         <a href="Infor_View.aspx?ArticleId=2924" class="">
     
     张时善同志在安全生产工作现场会上...</a><font color="black">
     </td>
       
       </tr>
     </table></td>
    
     
  </tr>
  
  <tr>
     <td height="23" align="left" class="td_hui"><table width="100%" border="0" cellspacing="0" cellpadding="0">
      
       <tr>
         <td width="85%">·
         
         <a href="Infor_View.aspx?ArticleId=2687" class="">
     
     张时善同志在全市暑期安全生产工作...</a><font color="black">
     </td>
       
       </tr>
     </table></td>
    
     
  </tr>
  
我要做个新闻采集功能,我要采集别的网站的新闻,上面是网站查看源文件的代码,我要采集的是 

<a href="Infor_View.aspx?ArticleId=2687" class="">
     
     张时善同志在全市暑期安全生产工作...</a>

求正则高手帮助,谢谢。。。 --------------------编程问答--------------------   using (System.IO.StreamReader sr = new System.IO.StreamReader("D:\\1.txt", System.Text.Encoding.Default))
            {
                string str = sr.ReadToEnd();
                str = System.Text.RegularExpressions.Regex.Match(str, @"<a [^>]*2687[^>]*>((?!</a>)[\s\S])*</a>").Value;
                Console.WriteLine(str);
            } --------------------编程问答--------------------

     string html = @"<tr>
  <td height=""23"" align=""left"" class=""td_hui""><table width=""100%"" border=""0"" cellspacing=""0"" cellpadding=""0"">
    
  <tr>
  <td width=""85%"">·
    
  <a href=""Infor_View.aspx?ArticleId=2924"" class="""">
    
  张时善同志在安全生产工作现场会上...</a><font color=""black"">
  </td>
    
  </tr>
  </table></td>
    
    
  </tr>
   
  <tr>
  <td height=""23"" align=""left"" class=""td_hui""><table width=""100%"" border=""0"" cellspacing=""0"" cellpadding=""0"">
    
  <tr>
  <td width=""85%"">·
    
  <a href=""Infor_View.aspx?ArticleId=2687"" class="""">
    
  张时善同志在全市暑期安全生产工作...</a><font color=""black"">
  </td>
    
  </tr>
  </table></td>
    
    
  </tr>
   
我要做个新闻采集功能,我要采集别的网站的新闻,上面是网站查看源文件的代码,我要采集的是  

<a href=""Infor_View.aspx?ArticleId=2687"" class="""">
    
  张时善同志在全市暑期安全生产工作...</a>

求正则高手帮助,谢谢。。。
                         ";
            html = Regex.Replace(html, @"\r|\n|\t", "");
            Regex reg = new Regex(@"\<a.*?ArticleId.*?\/a\>", RegexOptions.IgnoreCase);
            List<string> links = new List<string>();
            foreach (Match m in reg.Matches(html))
            {
                links.Add(m.Value);
            }
            foreach (var r in links)
            {
                Response.Write(r + "<br>");
            }
        }


结果
<a href="Infor_View.aspx?ArticleId=2924" class="">      张时善同志在安全生产工作现场会上...</a>
共三条。
--------------------编程问答--------------------

      string   html   =   @ " <tr> 
    <td   height= " "23 " "   align= " "left " "   class= " "td_hui " "> <table   width= " "100% " "   border= " "0 " "   cellspacing= " "0 " "   cellpadding= " "0 " "> 
        
    <tr> 
    <td   width= " "85% " "> · 
        
    <a   href= " "Infor_View.aspx?ArticleId=2924 " "   class= " " " "> 
        
    张时善同志在安全生产工作现场会上... </a> <font   color= " "black " "> 
    </td> 
        
    </tr> 
    </table> </td> 
        
        
    </tr> 
      
    <tr> 
    <td   height= " "23 " "   align= " "left " "   class= " "td_hui " "> <table   width= " "100% " "   border= " "0 " "   cellspacing= " "0 " "   cellpadding= " "0 " "> 
        
    <tr> 
    <td   width= " "85% " "> · 
        
    <a   href= " "Infor_View.aspx?ArticleId=2687 " "   class= " " " "> 
        
    张时善同志在全市暑期安全生产工作... </a> <font   color= " "black " "> 
    </td> 
        
    </tr> 
    </table> </td> 
        
        
    </tr> 
      
我要做个新闻采集功能,我要采集别的网站的新闻,上面是网站查看源文件的代码,我要采集的是     

<a   href= " "Infor_View.aspx?ArticleId=2687 " "   class= " " " "> 
        
    张时善同志在全市暑期安全生产工作... </a> 

求正则高手帮助,谢谢。。。 
                                                  "; 
                        html   =   Regex.Replace(html,   @ "\r|\n|\t ",   " "); 
                        Regex   reg   =   new   Regex(@ "\ <a.*?ArticleId.*?\/a\> ",   RegexOptions.IgnoreCase); 
                        List <string>   links   =   new   List <string> (); 
                        foreach   (Match   m   in   reg.Matches(html)) 
                        { 
                                links.Add(m.Value); 
                        } 
                        foreach   (var   r   in   links) 
                        { 
                                Response.Write(r   +   " <br> "); 
                        } 
                } 
--------------------编程问答-------------------- --------------------编程问答-------------------- (?is)<td width="85%">.*?(<a[^>]*>.*?</a>)<font color="black">.*?</td> --------------------编程问答-------------------- (?is)<a\b[^>]*?href=(["']?)[^"']*?ArticleId=\d+\1[^>]*?>.*?</a>
这个正则只适合你给的html,应该有一些更明确的限定 --------------------编程问答--------------------
引用 3 楼  的回复:
C# code


      string   html   =   @ " <tr> 
    <td   height= " "23 " "   align= " "left " "   class= " "td_hui " "> <table   width= " "100% " "   border= " "0 " "   cellspacing= " "0 " "   cell……


谢谢你,我想抓取 http://www.tjsafety.gov.cn 这个网站的新闻,你的出来好像有很多其他的内容,我还是 要下面这样新闻的内容,不要图片新闻,你给看看如何获取到,谢谢了!!!
<a href=  "Infor_View.aspx?ArticleId=2687 "    class= " "> 
张时善同志在全市暑期安全生产工作... </a> 
--------------------编程问答--------------------
引用 5 楼  的回复:
(?is)<td width="85%">.*?(<a[^>]*>.*?</a>)<font color="black">.*?</td>


谢谢你,我想抓取 http://www.tjsafety.gov.cn 这个网站的新闻,你的出来好像有很多其他的内容,我还是 要下面这样新闻的内容,不要图片新闻,你给看看如何获取到,谢谢了!!!
<a href=  "Infor_View.aspx?ArticleId=2687 "    class= " "> 
张时善同志在全市暑期安全生产工作... </a> 
--------------------编程问答--------------------
引用 5 楼  的回复:
(?is)<td width="85%">.*?(<a[^>]*>.*?</a>)<font color="black">.*?</td>



谢谢你,我想抓取 http://www.tjsafety.gov.cn 这个网站的新闻,你的出来好像有很多其他的内容,我还是 要下面这样新闻的内容,不要图片新闻,你给看看如何获取到,谢谢了!!!
<a href=  "Infor_View.aspx?ArticleId=2687 "    class= " "> 
张时善同志在全市暑期安全生产工作... </a> 
--------------------编程问答-------------------- 修改如下
(?is)<td width="85%">\s*?(<a[^>]*>.*?</a>)<font color="black">.*?</td>



--------------------编程问答--------------------
引用 6 楼  的回复:
(?is)<a\b[^>]*?href=(["']?)[^"']*?ArticleId=\d+\1[^>]*?>.*?</a>
这个正则只适合你给的html,应该有一些更明确的限定



谢谢你,我想抓取 http://www.tjsafety.gov.cn 这个网站的新闻,你的出来好像有很多其他的内容,我还是 要下面这样新闻的内容,不要图片新闻,你给看看如何获取到,谢谢了!!!
<a href= "Infor_View.aspx?ArticleId=2687 " class= " ">  
张时善同志在全市暑期安全生产工作... </a>  
 
 
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,