// 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)(?<function>\w+) \( # Matches function name
(?<parameters> # Simple param type.
(?<data> # Simple data type: inner function or word (keep this order!).
(?R) | # Or recursive this Regexp to match inner functions.
\w+ # Matches a simple word.
)
( # Match additional parameters:
\s*,\s* # Match comma, but skip spaces.
\g<parameters> # Recurse additional parameters.
)* # Zero or more additional parameters.
)? # Parameters are optional
\)").unwrap();
let string = "color()
color(a)
color(a, a)
color(a())
color(a(b()))
color(a, a())
color(a, a(), b)
color(a(), b)
color(a, b, c(d, e(f, g()), h))";
// 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/