c# - Match exact closest string with regex -
i have string:
test. <div> <table style="color:blue;"><tbody><!--start space comments summary--> <tr><td colspan="2">space comments summary</td></tr> <tr><td style="min-width:200px;">area/room</td> <td style="max-width:300px;text-align:left;">comments</td> </tr><tr><td style="min-width:200px;">bathroom</td> <td style="max-width:300px;text-align:left;">some comment</td></tr> <!--end space comments summary--></tbody></table> <div> <table style="color:blue;"><tbody><!--start space summary--> <tr><td colspan="2">space summary</td></tr><tr> <td style="min-width:200px;">space</td> <td style="max-width:300px;text-align:right;">installed price</td></tr> <tr><td style="min-width:200px;">bathroom</td> <td style="max-width:300px;text-align:right;">$2,355.97</td></tr> <!--end space summary--></tbody></table> <br><br><br><div>some text.</div></div></div>
i want select regex table has comments <!--start space summary>
, <!--end space summary-->
.
i tried @"<table.*?><tbody.*?><!--start space summary>.*?<!--end space summary--></tbody></table>"
, selects both tables in string.
edit: question doesn't have precisely html. same question stand if had string:
some text blah blah 1 text blah blah two.
and want select some text blah blah two
pattern some text.*?two
.
string test = @"test. <div> <table style=""color:blue;""><tbody><!--start space comments summary--> <tr><td colspan=""2"">space comments summary</td></tr> <tr><td style=""min-width:200px;"">area/room</td> <td style=""max-width:300px;text-align:left;"">comments</td> </tr><tr><td style=""min-width:200px;"">bathroom</td> <td style=""max-width:300px;text-align:left;"">some comment</td></tr> <!--end space comments summary--></tbody></table> <div> <table style=""color:blue;""><tbody><!--start space summary--> <tr><td colspan=""2"">space summary</td></tr><tr> <td style=""min-width:200px;"">space</td> <td style=""max-width:300px;text-align:right;"">installed price</td></tr> <tr><td style=""min-width:200px;"">bathroom</td> <td style=""max-width:300px;text-align:right;"">$2,355.97</td></tr> <!--end space summary--></tbody></table> <br><br><br><div>some text.</div></div></div>"; matchcollection matches = regex.matches(test, @"<table(?!.*<table).*?<!--start space summary-->.*?<!--end space summary-->.*?table>", regexoptions.singleline);
the idea use (?!.*<table)
tell regex engine the text within should not contain table anchor.
Comments
Post a Comment