62,074
社区成员
发帖
与我相关
我的任务
分享
if()
{
11111
if()
{
2222
if()
{
3333
}
}
}
if()
{
44444
if()
{
55555
if()
{
66666
}
}
}
//通过ID处理嵌套HTML标签
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" };
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();
string str = @"if(a)
{
11111
if(b)
{
2222
if(c)
{
3333
}
}
}
if(d)
{
44444
if(e)
{
55555
if(d)
{
66666
}
}
}
";
string ifExp = "a";
string pattern = @"if\(" + ifExp + @"\)\s*\{.*?(((?'Open'if\([^\)]*\)\s*\{)[^\{\}]*)+((?'-Open'\}).*?)+)*(?(Open)(?!))\}"
//前提是 if中只有if嵌套,不能有别的{}, 而且if中不能再有(),可以通过改变if当中的条件ifExp来确定要取得是哪个if