求教一个正则表达式,在一个html文本块中查找出有多少对td标签,并查出标签中的指定内容。
有如下一个文本块:
<table txsTableStyle>
<tr>
<td txsTdImageStyle1>txsTdImageData1</td>
<td txsTdImageStyle2>txsTdImageData2</td>
<td txsTdImageStyle3>txsTdImageData3</td>
</tr>
<tr>
<td colspan="3" txsTdTextStyle1>txsTdTextData1</td>
</tr>
<tr>
<td txsTdImageStyle4>txsTdImageData4</td>
<td txsTdImageStyle5>txsTdImageData5</td>
<td txsTdImageStyle6>txsTdImageData6</td>
</tr>
<tr>
<td colspan="3" txsTdTextStyle2>txsTdTextData2</td>
</tr>
……(没有写完)
</table>问题一:如何找出上面这段代码中,有多少对<td></td>,并将各对显示出各对来,如显示效果如下;
————————————————————————————————————————————
存在的td标识有n对,显示如下:
<td txsTdImageStyle1>txsTdImageData1</td>
<td txsTdImageStyle2>txsTdImageData2</td>
<td txsTdImageStyle3>txsTdImageData3</td>
……
<td colspan="3" txsTdTextStyle2>txsTdTextData2</td>
————————————————————————————————————————————
问题二:将txsTdImageStyle1……txsTdImageStylen分离并显示出来,如显示效果如下;
————————————————————————————————————————————
本文本块存在:txsTdImageStyle1……txsTdImageStyle6。
————————————————————————————————————————————
问题三:将txsTdTextStyle1……txsTdTextStylen分离并显示出来,如显示效果如下;
————————————————————————————————————————————
本文本块存在:txsTdTextStyle1……txsTdTextStyle2。
————————————————————————————————————————————
问题四:将txsTdImageData1……txsTdImageDatan分离并显示出来,如显示效果如下;
————————————————————————————————————————————
本文本块存在:txsTdImageData1……txsTdImageData6。
————————————————————————————————————————————
问题五:将txsTdTextData1……txsTdTextDatan分离并显示出来,如显示效果如下;
————————————————————————————————————————————
本文本块存在:txsTdTextData1,txsTdTextData2。
————————————————————————————————————————————谢谢。
有如下一个文本块:
<table txsTableStyle>
<tr>
<td txsTdImageStyle1>txsTdImageData1</td>
<td txsTdImageStyle2>txsTdImageData2</td>
<td txsTdImageStyle3>txsTdImageData3</td>
</tr>
<tr>
<td colspan="3" txsTdTextStyle1>txsTdTextData1</td>
</tr>
<tr>
<td txsTdImageStyle4>txsTdImageData4</td>
<td txsTdImageStyle5>txsTdImageData5</td>
<td txsTdImageStyle6>txsTdImageData6</td>
</tr>
<tr>
<td colspan="3" txsTdTextStyle2>txsTdTextData2</td>
</tr>
……(没有写完)
</table>问题一:如何找出上面这段代码中,有多少对<td></td>,并将各对显示出各对来,如显示效果如下;
————————————————————————————————————————————
存在的td标识有n对,显示如下:
<td txsTdImageStyle1>txsTdImageData1</td>
<td txsTdImageStyle2>txsTdImageData2</td>
<td txsTdImageStyle3>txsTdImageData3</td>
……
<td colspan="3" txsTdTextStyle2>txsTdTextData2</td>
————————————————————————————————————————————
问题二:将txsTdImageStyle1……txsTdImageStylen分离并显示出来,如显示效果如下;
————————————————————————————————————————————
本文本块存在:txsTdImageStyle1……txsTdImageStyle6。
————————————————————————————————————————————
问题三:将txsTdTextStyle1……txsTdTextStylen分离并显示出来,如显示效果如下;
————————————————————————————————————————————
本文本块存在:txsTdTextStyle1……txsTdTextStyle2。
————————————————————————————————————————————
问题四:将txsTdImageData1……txsTdImageDatan分离并显示出来,如显示效果如下;
————————————————————————————————————————————
本文本块存在:txsTdImageData1……txsTdImageData6。
————————————————————————————————————————————
问题五:将txsTdTextData1……txsTdTextDatan分离并显示出来,如显示效果如下;
————————————————————————————————————————————
本文本块存在:txsTdTextData1,txsTdTextData2。
————————————————————————————————————————————谢谢。
<table\s+\w+>
string content = @"<table txsTableStyle>
<tr>
<td txsTdImageStyle1>txsTdImageData1</td>
<td txsTdImageStyle2>txsTdImageData2</td>
<td txsTdImageStyle3>txsTdImageData3</td>
</tr>
<tr>
<td colspan=""3"" txsTdTextStyle1>txsTdTextData1</td>
</tr>
<tr>
<td txsTdImageStyle4>txsTdImageData4</td>
<td txsTdImageStyle5>txsTdImageData5</td>
<td txsTdImageStyle6>txsTdImageData6</td>
</tr>
<tr>
<td colspan=""3"" txsTdTextStyle2>txsTdTextData2</td>
</tr>
……(没有写完)
</table>";
Regex htmlRegex = new Regex(@"<td[^>]*>.*?</td>", RegexOptions.IgnoreCase | RegexOptions.Compiled);MatchCollection mc = htmlRegex.Matches(content);
string[] div = new string[mc.Count];
for (int i=0; i<mc.Count; i++)
{
Console.WriteLine(mc[i].Value);
}要列出全部<td txsTdImageStyle1>只需要把
@"<td[^>]*>.*?</td>"
改成相应的
@"<td txsTdImageStyle[^>]*>.*?</td>"
就行了