// 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#"(?x)editor\s*\{[^{}"]*
# Repeatedly match the key-value pairs
(?:
# Match from opening to closing double quote for the 'key'
"[^\\"]*
# Handle escaped characters: \ followed by another character
(?: \\. [^\\"]* )*
"
# Then any whitespace between the 'key' and 'value'
\s+
# Match from opening to closing double quote for the 'value'
"[^\\"]*
# Handle escaped characters: \ followed by another character
(?: \\. [^\\"]* )*
"
# Match everything (especially whitespace) except these chars:
[^{}"]*
|
# Key name may or may not be within quotes
"? \w+ "? [^{}"]*
\{[^{}"]*
(?:
"[^\\"]* (?: \\. [^\\"]* )* " \s+ "[^\\"]* (?: \\. [^\\"]* )* "
[^{}"]*
)*
\} [^{}"]*
)*
\}"#).unwrap();
let string = "\"entity\"
{
\"id\" \"5040044\"
\"classname\" \"weapon_defibrillator_spawn\"
\"angles\" \"0 0 0\"
\"body\" \"0\"
\"disableshadows\" \"0\"
\"skin\" \"0\"
\"solid\" \"6\"
\"spawnflags\" \"3\"
\"origin\" \"449.47 5797.25 2856\"
editor
{
\"color\" \"0 0 200\"
\"visgroupshown\" \"1\"
\"visgroupautoshown\" \"1\"
\"logicalpos\" \"[-13268 14500]\"
\"helptext\" \"This is a \\\"help\\\" menu.\"
}
editor
{
\"color\" \"0 0 200\"
\"visgroupshown\" \"1\"
\"visgroupautoshown\" \"1\"
\"logicalpos\" \"[-13268 14500]\"
}
editor
{
\"color\" \"0 0 200\"
\"visgroupshown\" \"1\"
\"visgroupautoshown\" \"1\"
\"logicalpos\" \"[-13268 14500]\"
\"specialchars\" \"}\"
}
editor
{
\"child\" \"0 0 200\"
\"visgroupshown\" \"1\"
\"visgroupautoshown\" \"1\"
\"logicalpos\" \"[-13268 14500]\"
subgroup
{
\"child\" \"42 42 42\"
}
}
}
";
// 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/