// 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)<img.+?class=["'].*?hero-class.*?["'].*?src=["'](.+?)["'].*?>"#).unwrap();
let string = "// it should match but it didn't
<img src=\"image.jpg\" class=\"hero-class\">
// it shouldn't match class 'nohero-class-dont-match'
<img class=\"nohero-class-dont-match\" src=\"image.jpg\">
// it shouldn't match custom tag 'imgs-tag'
<imgs-tag class=\"hero-class\" src=\"image.jpg\">
// it shouldn't match two tags and text between them
<img src=\"foo.jpg\" class=\"hero-class\"> any text <img src=\"image.jpg\">
// it should match whole src attribute, not just part
<img class=\"hero-class\" src=\"mcdonald's.jpg\">
// it shouldn't match custom attribute 'data-src'
<img class=\"hero-class\" data-src=\"custom info\" src=\"image.jpg\">
";
// 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/