// 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#"(?x)(<a.*?(?:href=")?)?(https?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com(?:\/embed\/|\/v\/|\/watch?.*?v=))([\w\-]{10,12})(.*<\/a>?)?"#).unwrap();
let string = "<p> </p>
<p> </p>
https://www.youtube.com/watch?v=yCrjqNJgkNA
<p> </p>
<p> </p>
www.youtube.com/watch?v=TkRYM3Vt5v4
<p> </p>
<p> </p>
<p> </p>
http://www.youtube.com/watch?v=nfPYWD3Hxd4
<p><a href=\"https://www.youtube.com/watch?v=xV-ny2fNauA\">https://www.youtube.com/watch?v=xV-ny2fNauA</a></p>
<p><a href=\"https://www.youtube.com/watch?v=4Quu-22CqHg\">https://www.youtube.com/watch?v=4Quu-22CqHg</a></p>
<p><a href=\"https://www.youtube.com/watch?v=5BlLmJxNV0s\">https://www.youtube.com/watch?v=5BlLmJxNV0s</a></p>
<p><a href=\"https://www.youtube.com/watch?v=wtMoYjUIpDk\">https://www.youtube.com/watch?v=wtMoYjUIpDk</a></p>
<p><a href=\"https://www.youtube.com/watch?v=9U35vNlwj3o\">https://www.youtube.com/watch?v=9U35vNlwj3o</a></p>
<p><a href=\"https://www.youtube.com/watch?v=lFITZBBz_lk\">https://www.youtube.com/watch?v=lFITZBBz_lk</a></p>
<p><a href=\"https://www.youtube.com/watch?v=jjIp1qSkfgg\">https://www.youtube.com/watch?v=jjIp1qSkfgg</a></p>
<p><a href=\"https://www.youtube.com/watch?v=KM5g6wpY8a4\">https://www.youtube.com/watch?v=KM5g6wpY8a4</a></p>";
// 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/