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

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

your answer gave me another idea, i can use ids instead of placeholders, let me edit your regex for that and test it.
any idea why this regex fails to match tables on id \s*?<table\s*.*id=\"t2\">.*</table>
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
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.