用正則表達式匹配HTML\XML等文件中的標簽


測試用HTML源文件:

View Code
<table cellspacing=0 cellpadding=0 border=0 width=100%>
  <tr>
         <td width="145px" valign=top><center>
     <div style="border:1px solid #588acb;padding:1px">    
       <a href="http://game.china.com/online/wmworld/news/11099107/20121226/17602912.html" target="_blank" style="text-decoration:none"><img src="http://news.youdao.com/improxy?w=145&h=109&id=b2cd45d7ccce332b"></a>
     </div>
   </center></td>
      <td valign=top>      <table cellspacing=0 cellpadding=0 border=0 width=98% align=right>
       <tr>
         <td colspan=2 style="padding-top:5px">
           <span id="title0"><a id="hitURL0" href="http://game.china.com/online/wmworld/news/11099107/20121226/17602912.html" target="_blank">《完美世界:天界的召喚》繽紛聖誕總動員</a></span>         
         </td>
       </tr>
       <tr>
         <td colspan=2 style="padding-top:5px">
           <font size="-1"><span style="color:#636563"><nobr>[ 中華網 1小時前]</nobr></span><BR>
           經過了平安夜和聖誕節,節日的氣氛被推到了最高點!《完美世界:天界的召喚》為玩家准備了精彩紛呈的聖誕節活動,而玩家也紛紛借着這個特殊的日子,三五成群的舉辦着各種小聚會,在完美大陸中享受着這個浪漫的聖誕...</font>
         </td>
       </tr>
       <tr>
         <td colspan=2 style="padding-top:10px">         
         <font size="-1">
         <img src="http://news.youdao.com/images/tranpointsymbol.png" 
           border="0" alt="" align=absmiddle><span id="title0"><a id="hitURL0" 
           href="http://www.265g.com/news/gamenews/300073.html" target="_blank">聖誕元旦大狂歡360uu《盛世三國》雙蛋大禮齊獻映</a></span> <span 
           style="color:#636563"><nobr>[ 265G 1小時前]</nobr></span><br><img src="http://news.youdao.com/images/tranpointsymbol.png" 
           border="0" alt="" align=absmiddle><span id="title0"><a id="hitURL0" 
           href="http://life.qianlong.com/36311/2012/12/26/7144@8406202.htm" target="_blank">貝立茲攜手浦南幼兒園舉辦趣味英語聖誕派對</a></span> <span 
           style="color:#636563"><nobr>[ 千龍新聞網 3小時前]</nobr></span><br></font>         
         <div><img src="http://news.youdao.com/images/tranzhuantisymbol.png" border="0" alt="" align=absmiddle><nobr><font size=-1><a style="color:#008000;" href="top?item=1b620d66539e81dd">查看專題 (780篇新聞 )</a></font></nobr></div>
         </td>
         <td valign=bottom>&nbsp;</td>            
       </tr>
     </table>    </td> 
  </tr>
</table>

一、匹配所有標簽

\<.[^<>]*\>

思路:

1、首先匹配<和>,得到"\<.*\>",(\為轉義,<和>加不加都可以)但出現多個標簽在一組中。

2、排除中間的<和>,表達式改為"\<.[^<>]*\>"。

3、如果想得到不包含屬性的所有標簽,可以加個字符數限制,根據標簽字符數,表達式改為“\<\S[^<>]{0,12}\>”。

二、匹配指定標簽

匹配所有DIV標簽 \<\bdiv.*\<\/div\b\>

思路:

1、以<div 開頭,得到“\<\bdiv”。

2、以<\div>結束,得到“\<\/div\b\>”,\\注意兩次\b的使用,\b用於匹配位於詞開始處的詞邊界和用於匹配詞結尾處的詞邊界。

3、連接兩個表達式,中間填充任意字符,\<\bdiv.*\<\/div\b\>。

感慨:正則表達式真強大。

 

20130123補充

提取標簽中的內容

>.[^<>]+<

<li><a href="http://sports.sina.com.cn"target="_blank">新浪</a></li>
<li><a href="http://news.yule.com.cn"target="_blank">中國娛樂網</a></li>
<li><a href="http://pzh.scol.com.cn"target="_blank">四川在線</a></li>
<li><a href="http://news.0755car.com"target="_blank">車神榜</a></li>

結果:

>新浪<

>中國娛樂網<

>四川在線<

>車神榜<

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM