// 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#"(?sx)^((?!\bfruit\b).)* # nothing with "fruit" before
( # then, either:
# 1. fruit apple, followed by things that are not "fruit", followed by fruit banana, non-fruits, and then fruit cherry, or vice versa
\bfruit\b\ apple((?!\bfruit\b).)*(\bfruit\b\ banana((?!\bfruit\b).)*fruit\ cherry|fruit\ cherry((?!\bfruit\b).)*fruit\ banana)
|
# 2. Same with banana at the beginning
fruit\ banana((?!\\bfruit\b).)*(fruit\ apple((?!\bfruit\b).)*fruit\ cherry|fruit\ cherry((?!\bfruit\b).)*fruit\ apple)
|
# 3. And with cherry
fruit\ cherry((?!\bfruit\b).)*(fruit\ apple((?!\bfruit\b).)*fruit\ banana|fruit\ banana((?!\bfruit\b).)*fruit\ apple)
)
((?!\bfruit\b).)*$ # no more fruit"#).unwrap();
let string = "fruit apple
some other config line
############
fruit banana
fruit cherry
other config 1
other config";
// 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/