// 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)^rgba?\((?:(?:(?:[0-9]{1,3} ){2}[0-9]{1,3}|(?:[0-9]{1,3}% ){2}[0-9]{1,3}%)(?: \/ (?:[0-9]{1,3}%|1|0|0?\.[0-9]+))?|(?:(?:[0-9]{1,3}, ){2}[0-9]{1,3}|(?:[0-9]{1,3}%, ){2}[0-9]{1,3}%)(?:, (?:[0-9]{1,3}%|1|0|0?\.[0-9]+))?)\)$").unwrap();
let string = "// invalid
rgb()
rgba()
rgb(123)
rgb(11 22 )
rgb(11 22 2 / 03)
rgb(11 22 2 / 10)
rgb(1 2 3 / 0.34%)
rgb(11% 22 33)
rgb(111 222% 333)
rgb(1 22 333 )
rgb(1 2 3, 1)
rgb(1 2, 3 / 1)
// valid
rgb(111 22 333)
rgb(1 2 3)
rgb(11 22 33)
rgb(111 222 333)
rgb(1 22 333)
rgb(1 2 3 / 1)
rgb(11 22 2 / 0)
rgb(1 2 3 / 0.34)
rgb(1 2 3 / .34)
rgb(1 2 3 / 0.34)
rgb(1 2 3 / 1%)
rgb(1 2 3 / 10%)
rgb(1 2 3 / 100%)
rgb(11 22 2 / 0%)
rgb(1 2 3 / 34%)
rgb(1 2 3 / 4%)
rgb(111 22 333)
rgb(1% 2% 3%)
rgb(11% 22% 2% / 0)
rgb(12%, 0%, 100%)
rgb(111, 22, 333)
rgb(1, 2, 3)
rgb(1, 2, 3, 1)
";
// 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/