// 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"(?s)(?:^|\n)##\s[^\n]*\n+(.*?)(?=\n##?\s|$)").unwrap();
let string = "# This is an H1
content
content
content
## This is an H2 that ends in another H2
matched content 1
matched content 1
matched content 1
## This is an H2 that ends in an H1
matched content 2
matched content 2
matched content 2
# This is an H1, it's content doesn't belong to any H2
content
content
content
## This is an H2 that goes on until the end of the file
matched content 3
matched content 3
### This H3 belongs to the H2 above
matched content 4
matched content 4";
// result will be an iterator over tuples containing the start and end indices for each match in the string
let result = regex.captures_iter(string);
for mat in result {
println!("{:?}", mat);
}
}
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/