我正在尝试创建返回表或表数组的正则表达式。到目前为止我已经
了 @"<table>^<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)$</table>"
html可以是
<table>
<p id='p1'></p>
</table>
<table>
<p>abc</p>
</table>
例如,如果我运行以下代码
string str = "<table><p id='p1'></p></table><table><p>abc</p></table>";
Regex r = new Regex(@"/<table>^<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)$</table>/");
MatchCollection s = r.Matches(str);
Response.Write(s.Count);
然后它应该写“2”,因为有两个表。
上述正则表达式未按预期工作。用于解析html的正则表达式似乎没问题,但是我很难将hge和regex的正则表达式组合起来封装html(封装html元素的表)
答案 0 :(得分:1)
建议使用Html Agility Pack:
HtmlDocument htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(html);
var nodes = htmlDocument.DocumentNode.SelectNodes("//table");