// 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)^(?=\d+[a-z]+(?:-\d+[a-z]+)*$)(?>(?<gal>\d+)gal-?)?(?>(?<qt>\d)+qt-?)?(?>(?<pt>\d+)pt-?)?(?>(?<cup>\d+)cup-?)?(?>(?<oz>\d+)oz-?)?$").unwrap();
let string = "1gal
1qt-2cup
1gal-6oz
1gal
6oz
1gal-1qt-2pt-2cup-5oz
1gal-1qt-2oz
1gal-1qt-2pt-2cup-5o
1gal-1qt-2oz-2cup
1gal-1qt-2oz-2cup
-1gal-1qt-2oz
1gal-1qt-2oz-
sfgsdfgdsfaf
1gal-3qt-5a
1qt2cup
1gal2qt-3pt
";
// 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/