using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"<h1>([^<]*)<\/h1>\s*\n([\w\W]*?)(([^\n\S]*)<td\s.*?<\/td>(\n))(?=\s*<\/tr>)|(?<=<h1>([^<]*)<\/h1>[\w\W]*?)(([^\n\S]*)<td\s.*?<\/td>(\n))(?=\s*<\/tr>)";
string substitution = @"$2$3$4$7$8<td class=""two"">$1$6</td>$5$9";
string input = @"<h1>TITLE OF HEAD 1</h1>
<table>
<tbody>
<tr>
<td class=""one"">ITEM 1, AFTER HEAD 1</td>
</tr>
<tr>
<td class=""one"">ITEM 2, AFTER HEAD 1</td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr>
<td class=""one"">ITEM 3, AFTER HEAD 1</td>
</tr>
<tr>
<td class=""one"">ITEM 4, AFTER HEAD 1</td>
</tr>
<tr>
<td class=""one"">ITEM 5, AFTER HEAD 1</td>
</tr>
</tbody>
</table>
<h1>TITLE OF HEAD 2</h1>
<table>
<tbody>
<tr>
<td class=""one"">ITEM 6, AFTER HEAD 2</td>
</tr>
</tbody>
</table>
<h1>TITLE OF HEAD 3</h1>
<table>
<tbody>
<tr>
<td class=""one"">ITEM 7, AFTER HEAD 3</td>
</tr>
<tr>
<td class=""one"">ITEM 8, AFTER HEAD 3</td>
</tr>
<tr>
<td class=""one"">ITEM 9, AFTER HEAD 3</td>
</tr>
<tr>
<td class=""one"">ITEM 10, AFTER HEAD 3</td>
</tr>
</tbody>
</table>
<h1>TITLE OF HEAD 4</h1>
<table>
<tbody>
<tr>
<td class=""one"">ITEM 11, AFTER HEAD 4</td>
</tr>
<tr>
<td class=""one"">ITEM 12, AFTER HEAD 4</td>
</tr>
</tbody>
</table>
";
Regex regex = new Regex(pattern);
string result = regex.Replace(input, substitution);
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for C#, please visit: https://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex(v=vs.110).aspx