比如一段字符串(即是一个网页解析输出后的html代码)
<html>
<head></head>
<body>
<table class="ptable">
xxx
</ptable>
</body>得到结果
<table class="ptable">xxx</ptable>
写一个函数
private string GetHtmlById(string InputHtml,string ClassId)
{
//这里面如何用正则或者其它方法,根据ClassId得到这一个ClassId的outerHtml?}
<html>
<head></head>
<body>
<table class="ptable">
xxx
</ptable>
</body>得到结果
<table class="ptable">xxx</ptable>
写一个函数
private string GetHtmlById(string InputHtml,string ClassId)
{
//这里面如何用正则或者其它方法,根据ClassId得到这一个ClassId的outerHtml?}
Regex reg=new Regex("(?is)<body[^>]*>(?<body>.*?)</body>");
string result=reg.Match("").Groups["body"].Value;
body里面还有其它很多内容的啊。
"<html>"+
"<head>"+
"</head>"+
"<body>"+
"<table class=\"ptable\">"+
"xxx"+
"</table>"+
"</body>"+
"</html>"; XmlDocument doc = new XmlDocument();
doc.LoadXml(html);
XmlNode node = doc.SelectSingleNode("//table[@class='ptable']");
Console.WriteLine(node.OuterXml);
{
string html = @"<html>
<head></head>
<body>
<table class=""ptable"">
xxx
</table>
</body>";
Console.WriteLine(GetHtmlById(html,"ptable"));
} public static string GetHtmlById(string InputHtml, string ClassId)
{
return Regex.Match(InputHtml, @"(?is)<(\w+)[^<>]*?class=""" + ClassId + @"""[^<>]*>((?<o><\1)|(?<-o></\1)|((?!</(\1)?).)*)*(?(o)(?!))</\1[^>]*>").Value;
}
public static string GetHtmlById(string InputHtml, string ClassId)
{
//return Regex.Match(InputHtml, @"(?is)<(\w+)[^<>]*?class=""" + ClassId + @"""[^<>]*>((?<o><\1)|(?<-o></\1)|((?!</(\1)?).)*)*(?(o)(?!))</\1[^>]*>").Value;
return Regex.Match(InputHtml, @"(?is)<table[^<>]*?class=""" + ClassId + @"""[^<>]*>((?<o><table)|(?<-o></table)|((?!</table?).)*)*(?(o)(?!))</table[^>]*>");
}