这类嵌套正则如何写?通过id属性获得一个标签块
需求:就是通过id获得一个html标签块。
要处理的字符串:
输入输出样例1
输入:"div1"
输出:
输入输出样例2
输入:"div2"
输出:
输入输出样例3
输入:"div3"
输出:
输入输出样例4
输入:"table1"
输出:
调试代码:
目前只实现没有嵌套的匹配--"div4"
[Quote=输出]
--------begin div1--------
o(╯□╰)o
--------end div1--------
--------begin div2--------
o(╯□╰)o
--------end div2--------
--------begin div3--------
o(╯□╰)o
--------end div3--------
--------begin div4--------
<div id="div4" style="width:100px"></div>
--------end div4--------
--------begin table1--------
o(╯□╰)o
--------end table1--------
[/Quote]
要处理的字符串:
<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>输入输出样例1
输入:"div1"
输出:
<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>输入输出样例2
输入:"div2"
输出:
<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>输入输出样例3
输入:"div3"
输出:
<div id="div3">
<table id="table1">
<tr>
<td>
<div id="div4" style="width:100p
x"></div>
</td>
</tr>
</table>
</div>输入输出样例4
输入:"table1"
输出:
<table id="table1">
<tr>
<td>
<div id="div4" style="width:100p
x"></div>
</td>
</tr>
</table>调试代码:
目前只实现没有嵌套的匹配--"div4"
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();
}
}
}[Quote=输出]
--------begin div1--------
o(╯□╰)o
--------end div1--------
--------begin div2--------
o(╯□╰)o
--------end div2--------
--------begin div3--------
o(╯□╰)o
--------end div3--------
--------begin div4--------
<div id="div4" style="width:100px"></div>
--------end div4--------
--------begin table1--------
o(╯□╰)o
--------end table1--------
[/Quote]
...全文
相关推荐
正则表达式匹配闭合HTML标签(支持嵌套)
先确定我们要解决的问题——从一段Html文本中找出特定id的标签的innerHTML,需要的朋友可以参考下
java正则替换img标签中src值的方法
今天小编就为大家分享一篇java正则替换img标签中src值的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
c标签遍历集合嵌套的List集合
c标签遍历集合嵌套的List集合
ThinkPHP模板Volist标签嵌套循环输出多维数组的方法
本文实例讲述了ThinkPHP模板Volist标签嵌套循环输出多维数组的方法。分享给大家供大家参考,具体如下: ThinkPHP 中对 volist 标签嵌套使用可实现多维数组的输出。 volist 嵌套使用 一般的二维数组,可以用 volist 标签直接循环输出。对于多维数组,则需要对其中的数组成员再次使用 volist 标签嵌套循环输出。 构建一个多维数组: $food = array(); $food[0]['fruits'][0]['fruits1'] = '苹果1'; $food[0]['fruits'][0]['fruits2'] = '桔子1'; $food[0]['fruit
用正则表达式格式化html标签的代码
用正则表达式格式化html标签的代码


