C#substring html内容

时间:2011-06-16 14:41:09

标签: c# .net asp.net regex string

我在每个表之前使用带有占位符的tinymce存储数据

输入数据时的编辑器视图:

#data1
[html table1] 

#data2
[html table2]

#data3
[html table3]

这存储在数据库中包含<p>标记的数据库中。

我想根据传递的参数剥离并获取html表。

string getTable(string placeholder)
{
     string content = db.getData();

     //placeholder = data1, return html table 1 substring data from content variable
     return [html table1]; //html string

    //placeholder = data2
     return [html table2]; //html string
}

我如何使用C#实现这一目标?

2 个答案:

答案 0 :(得分:1)

在这种情况下,您可以尝试使用正则表达式。虽然它不是完全证明(HTML不是常规语言),但如果你没有嵌套表,它应该可以正常工作。

string strRegex = @"(?<=#data1)\s*?<table.*?>.*</table>";
Regex myRegex = new Regex(strRegex, RegexOptions.Singleline);
string strTargetString = @"#data1 <table><tr><td> asdsad</td></tr></table>";

foreach (Match myMatch in myRegex.Matches(strTargetString))
{
  if (myMatch.Success)
  {
     // myMatch.Value contains table
  }
}

答案 1 :(得分:1)

我认为this regex might be reliable #data2([^#]+|#(?!data))+</table> (click to see the example),但这取决于你的输入,它可能会破坏。 You can't trust regex to parse html.

#data1
<table id="t1">
<tr><td>#</td></tr>
</table>

#data2
<table id="t2">
<tr><td>#</td></tr>
</table>

#data3
<table id="t3">
<tr><td>#</td></tr>
</table>

要按照您的ID could try <table.*?id=.t1.>([^<]|\<(?!/table))+</table>匹配该表。

相关问题