// 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#"(?m)(subject name=\"(?:Biology|chem102)\"[\s\S]+?)\r?\n^topic$|(^topic$\r?\n)"#).unwrap();
let string = "subject name=\"Biology\" value=\"12th\"
topic name=\"system\" value=\"anything\"
topic
subject name=\"mathematics\" value=\"12th\"
topic name=\"system\" value=\"anything\"
other topics =\"anything\" value=\"anything\"
topic
subject name=\"chem102\" value=\"12th\"
topic name=\"system\" value=\"anything\"
topic
subject name=\"ComputerSc\" value=\"12th\"
topic name=\"system\" value=\"anything\"
other topics =\"anything\" value=\"anything\"
topic";
let substitution = "\\1\\2\\n";
// 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/