// 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)['"]([ \w]+)?MATCH([ \w]+)?["']"#).unwrap();
let string = "I have a string like get(3,\"No MATCH\",obj). I want to check if the word MATCH is enclosed within quotes (either single quote or double quote). Here MATCH is enclosed within quotes, although not exactly as \"MATCH\", it is still a part of the text contained in quotes as \"No MATCH\".
I want to write a function which takes the word (MATCH) as argument and returns true if it is contained within quotes or false if not.
Following are some other input strings which needs to be checked by this function:
* get(1101,\"MATCH\",obj) --> return true since MATCH is within quotes
* get(255,'NO MATCH',obj) --> return true since MATCH is a part of text contained within quotes
* get(1111,\"\" , MATCH) ---> return false since MATCH is not contained within quotes";
// 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/