110,499
社区成员
发帖
与我相关
我的任务
分享
<html>
<body>
<div id="div1">
<div id="div2" style="background:Red;">
<div id="div3">
<table id="table1">
<tr>
<td>
<div id="div4" style="width:100p
x"></div>
</td>
</tr>
</table>
</div>
</div>
<div id="div5">
<a href="http://www.csdn.net">csdn</a>
</div>
</div>
<img src="http://www.csdn.net/Images/logo_csdn.gif"/>
</body>
</html>
<div id="div1">
<div id="div2" style="background:Red;">
<div id="div3">
<table id="table1">
<tr>
<td>
<div id="div4" style="width:100p
x"></div>
</td>
</tr>
</table>
</div>
</div>
<div id="div5">
<a href="http://www.csdn.net">csdn</a>
</div>
</div>
<div id="div2" style="background:Red;">
<div id="div3">
<table id="table1">
<tr>
<td>
<div id="div4" style="width:100p
x"></div>
</td>
</tr>
</table>
</div>
</div>
<div id="div3">
<table id="table1">
<tr>
<td>
<div id="div4" style="width:100p
x"></div>
</td>
</tr>
</table>
</div>
<table id="table1">
<tr>
<td>
<div id="div4" style="width:100p
x"></div>
</td>
</tr>
</table>
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string html = @"
<html>
<body>
<div id=""div1"">
<div id=""div2"" style=""background:Red;"">
<div id=""div3"">
<table id=""table1"">
<tr>
<td>
<div id=""div4"" style=""width:100px""></div>
</td>
</tr>
</table>
</div>
</div>
<div id=""div5"">
<a href=""http://www.csdn.net"">csdn</a>
</div>
</div>
<img src=""http://www.csdn.net/Images/logo_csdn.gif""/>
</body>
</html>";
Console.WriteLine(html);
string[] idList = { "div1", "div2", "div3", "div4", "table1" };
/* TODO : 这里发挥 */
string pattern = @"<([a-z]+)[^>]*\bid=(""|'){0}\2[^>]*></\1>";
foreach (string id in idList)
{
Match match = Regex.Match(html, string.Format(pattern, id),
RegexOptions.Singleline | RegexOptions.IgnoreCase);
Console.WriteLine("--------begin {0}--------", id);
if (match.Success)
Console.WriteLine(match.Value);
else
Console.WriteLine("o(╯□╰)o");
Console.WriteLine("--------end {0}--------", id);
}
Console.ReadKey();
}
}
}
<div id=div5>
<a href="http://www.csdn.net">csdn</a>
</div>
Match match = Regex.Match(html, string.Format(pattern, Regex.Escape(id)),
RegexOptions.Singleline | RegexOptions.IgnoreCase);
string html = @"
<html>
<body>
<div id=""div1"">
<div id=""div2"" style=""background:Red;"">
<div id=""div3"">
<table id=""table1"">
<tr>
<td>
<div id=""div4"" style=""width:100px""></div>
</td>
</tr>
</table>
</div>
</div>
<div id=div5>
<a href=""http://www.csdn.net"">csdn</a>
</div>
</div>
<img src=""http://www.csdn.net/Images/logo_csdn.gif""/>
</body>
</html>";
Console.WriteLine(html);
string[] idList = { "div1", "div2", "div3", "div4", "table1", "div5", "abc(def" };
/* TODO : 这里发挥 */
string pattern = @"<([a-z]+)(?:(?!id)[^<>])*id=([""']?){0}\2[^>]*>(?>(?<o><\1[^>]*>)|(?<-o></\1>)|(?:(?!</?\1).))*(?(o)(?!))</\1>";
foreach (string id in idList)
{
Match match = Regex.Match(html, string.Format(pattern, Regex.Escape(id)),
RegexOptions.Singleline | RegexOptions.IgnoreCase);
Console.WriteLine("--------begin {0}--------", id);
if (match.Success)
Console.WriteLine(match.Value);
else
Console.WriteLine("o(╯□╰)o");
Console.WriteLine("--------end {0}--------", id);
}
Console.ReadLine();