// 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"(?m)\bKEYWORD1\b(?:(?<=\bKeyWord2\b.*?)|(?=.*?\bKeyWord2\b))").unwrap();
let string = "KEYWORD1 is ahead of Keyword2
KEYWORD1 is ahead of Keyword2 and another KeyWord2
KeyWord2 is behind KEYWORD1
KEYWORD1 is ahead of KEYWORD1 and behind KeyWord2
KeyWord2 is twice of KeyWord2 and in the end KEYWORD1
KEYWORD1 is
";
// 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/