首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

怎样用正则表达式,获取html中源代码指定的部分,该怎么解决

2012-03-17 
怎样用正则表达式,获取html中源代码指定的部分假设我已获得了网页的源代码,怎么写正则表达式,获取指定id

怎样用正则表达式,获取html中源代码指定的部分
假设我已获得了网页的源代码,怎么写正则表达式,获取指定id 的div 部分的源代码,如获取下面的红色字体
---------------------------------
<div id="test">
testString
</div>
  <div>
  <asp:DataList id="DataList1"
  BorderColor="black"
  CellPadding="1"
  CellSpacing="4" HorizontalAlign="Center"
  RepeatColumns="4"
  RepeatLayout="Table"
  runat="server" ShowFooter="true" ShowHeader="true"
  width="100%">
  <HeaderTemplate>
  <table style="width: 100%">
  <tr>
  <td style="width:50%">
  地区编号</td>
  <td style="width:50%" align="left">
  地区名称</td>
  </tr>
  </table>
  </HeaderTemplate>
  <ItemTemplate>
  </ItemTemplate>
  </asp:DataList>  
  <input id="Button1" type="button" value="button" language="javascript" onclick="return Button1_onclick()" /></div>

  </form>
</body>
</html>

[解决办法]

try:
(\<div\s+id="test"\>[^</div>]*?\<\/div\>)


[解决办法]

C# code
using System.Text.RegularExpressions;Regex re = new Regex(@"(?<content><div\s*id=""?test""?\s*>[\s\S]*?</div>)");Match m = re.Match(str);m.Groups["content"];
[解决办法]
你去看看正则表达式怎么使用吧

热点排行