// 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)^(https?://)?(www\.)?youtu(\.be|be\.com)/(watch\?v=|embed/)?(?<id>[\w-]{11,})").unwrap();
let string = "https://youtube.com/watch?v=q9s_9RZ6lvw
https://www.youtube.com/watch?v=q9s_9RZ6lvw
https://youtu.be/q9s_9RZ6lvw
https://www.youtube.com/embed/q9s_9RZ6lvw
https://youtube.com/embed/q9s_9RZ6lvw
youtube.com/watch?v=q9s_9RZ6lvw
www.youtube.com/watch?v=q9s_9RZ6lvw
youtu.be/q9s_9RZ6lvw
youtube.com/embed/q9s_9RZ6lvw
http://youtube.com/watch?v=q9s_9RZ6lvw
http://www.youtube.com/watch?v=q9s_9RZ6lvw
http://youtu.be/q9s_9RZ6lvw
http://www.youtube.com/embed/q9s_9RZ6lvw
http://youtube.com/embed/q9s_9RZ6lvw
http://youtube.com/watch?v=q9s_9RZ6lvw&rel=1
http://www.youtube.com/watch?v=q9s_9RZ6lvw&autoplay=1
http://youtu.be/q9s_9RZ6lvw?ref=0
http://www.youtube.com/embed/q9s_9RZ6lvw?abc=xyz
http://youtube.com/embed/q9s_9RZ6lvw?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/