1

I have data stored using tinymce with placeholders before each table

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#?

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
rs.
  • 26,707
  • 12
  • 68
  • 90

2 Answers2

1

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
  }
}
Matthew Manela
  • 16,572
  • 3
  • 64
  • 66
1

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>.

Community
  • 1
  • 1
BrunoLM
  • 97,872
  • 84
  • 296
  • 452
  • your answer gave me another idea, i can use ids instead of placeholders, let me edit your regex for that and test it. – rs. Jun 16 '11 at 15:22
  • any idea why this regex fails to match tables on id \s*?.*
    – rs. Jun 16 '11 at 15:27