$re = '/<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>)/';
$str = '<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>
';
$subst = "$2$3$4$7$8<td class=\"two\">$1$6</td>$5$9";
$result = preg_replace($re, $subst, $str);
echo "The result of the substitution is ".$result;
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 PHP, please visit: http://php.net/manual/en/ref.pcre.php