// 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"(?mx)
(?<firstcapture> ~Start [^~]* ) #_(3)
(?<randomStuff> .*? ) #_(4)
(?<loop_first> #_(5 start)
(?:
~
(?: LOOP )
\d [^~]
)
( .*? ) # (1)
) #_(5 end)
(?:
(?<loop_middle> #_(6 start)
(?:
(?:
~
(?: LOOP )
\d [^~]
)
.*?
)*
) #_(6 end)
(?<loop_last> #_(7 start)
(?:
~
(?: LOOP )
\d [^~]
)
( .*? ) # (2)
) #_(7 end)
)?
(?<end> ~End [ ] stuff~ ) #_(8)
").unwrap();
let string = " BlahBlahBlah~Start capture~Yadda~Yadda~Yadda~LOOP1 hello1~LOOP2 hello2~LOOP3 hello3~End stuff~
BlahBlahBlah~Start capture~Yadda~Yadda~Yadda~LOOP1 hello1~LOOP2 hello2~End stuff~
BlahBlahBlah~Start capture~Yadda~Yadda~Yadda~LOOP1 hello1~LOOP3 hello3~End stuff~
BlahBlahBlah~Start capture~Yadda~Yadda~Yadda~LOOP1 hello1~End stuff~
";
let substitution = "${firstcapture}${randomStuff}${loop_last}${loop_middle}${loop_first}${end}";
// 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/