开发者

C# substring html content

开发者 https://www.devze.com 2023-03-13 06:07 出处:网络
I have data stored using tinymce with placeholders before each t开发者_如何学Goable Editor view when data is entered:

I have data stored using tinymce with placeholders before each t开发者_如何学Goable

Editor view when data is entered:

#data1
[html table1] 

#data2
[html table2]

#data3
[html table3]

this is stored in database wrapped with <p> tag in database.

I want to strip and get html table based on parameter passed.

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
}

How can i achieve this using C#?


I think this regex might be reliable #data2([^#]+|#(?!data))+</table> (click to see the example), but it depends on your input, it can break. 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>

To match the table by its ID you could try <table.*?id=.t1.>([^<]|\<(?!/table))+</table>.


You can try to use a regular expression in this case. While it won't be full proof (HTML is not a regular language) but if you don't have nested tables it should work fine.

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
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号