// 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"(?ui){\s*{\s*#\s*(?<operator>endcase|endswitch|endfor|endif|if|elif|else|for|switch|case|default)\s*(?<args>[^}]*)\s*}\s*}\s*(?<body>[^{]*(?:\s*{\s*{\s*#\s*(?!(?&operator))\s*[^}]*\s*}\s*}\s*[^{]*)*)").unwrap();
let string = "<div>
{{#if arg1.arg2}}
<span>{{#var arg3 static}}</span>
blablabla{{#for k, v in arg4}}
<p>{{#var k}}<span>{{#var v.arg5}}</span></p>
{{#if (k == \"test\" or v != 0) and k == 0}}
<span></span>
{{#elif k == 0}}
<p></p>
{{#else}}
<div></div>
{{#endif}}
{{#endfor}}
{{#endif}}
{{#switch arg6}}
{{#case 5}}
case 5
{{#case 6, 7, 8}}
case 6, 7, 8
{{#endcase}}
{{#default}}
def
{{#endcase}}
{{#endswitch}}
{{#for i as 0, 5}}
test i
{{#endfor}}";
// 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/