// include the latest version of the regex crate in your Cargo.toml
extern crate regex;
use regex::Regex;
fn main() {
let regex = Regex::new(r"<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>)").unwrap();
let string = "<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>
";
let substitution = "$2$3$4$7$8<td class=\"two\">$1$6</td>$5$9";
// result will be a String with the substituted value
let result = regex.replace_all(string, substitution);
println!("{}", 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 Rust, please visit: https://docs.rs/regex/latest/regex/