// 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"(?ims)(?<=(?:\[embed\])|(?:\[embed\]\s)|(?:))(https?:\/\/)(:?www\.)?(:?youtube\.com\/watch|youtu\.be\/)([\w\?=\&]+)(?=(?:\[\/embed\])|(?:\s\[\/embed\])|(?:))").unwrap();
let string = "bla https://www.youtube.com/watch?v=Vpg9yizPP_g
http://www.youtube.com/watch?v=Vpg9yizPP_g bla
[embed]https://www.youtube.com/watch?v=Vpg9yizPP_g[/embed] bla
[embed] https://www.youtube.com/watch?v=Vpg9yizPP_g [/embed]
[embed]http://www.youtube.com/watch?v=Vpg9yizPP_g[/embed]
[embed] http://www.youtube.com/watch?v=Vpg9yizPP_g [/embed]
bla https://youtube.com/watch?v=Vpg9yizPP_g bla
http://youtube.com/watch?v=Vpg9yizPP_g
[embed]https://youtube.com/watch?v=Vpg9yizPP_g[/embed]bla
bla[embed] https://youtube.com/watch?v=Vpg9yizPP_g [/embed]
[embed]http://youtube.com/watch?v=Vpg9yizPP_g[/embed]
[embed] http://youtube.com/watch?v=Vpg9yizPP_g [/embed]
https://www.youtube.com/watch?feature=player_embedded&v=Vpg9yizPP_g
http://www.youtube.com/watch?feature=player_embedded&v=Vpg9yizPP_g
[embed]https://www.youtube.com/watch?feature=player_embedded&v=Vpg9yizPP_g[/embed]
[embed] https://www.youtube.com/watch?feature=player_embedded&v=Vpg9yizPP_g [/embed]
[embed]http://www.youtube.com/watch?feature=player_embedded&v=Vpg9yizPP_g[/embed]
bla[embed] http://www.youtube.com/watch?feature=player_embedded&v=Vpg9yizPP_g [/embed]bla
https://youtube.com/watch?feature=player_embedded&v=Vpg9yizPP_g bla
bla http://youtube.com/watch?feature=player_embedded&v=Vpg9yizPP_g
[embed]https://youtube.com/watch?feature=player_embedded&v=Vpg9yizPP_g[/embed]
[embed] https://youtube.com/watch?feature=player_embedded&v=Vpg9yizPP_g [/embed]
[embed]http://youtube.com/watch?feature=player_embedded&v=Vpg9yizPP_g[/embed]
[embed] http://youtube.com/watch?feature=player_embedded&v=Vpg9yizPP_g [/embed]
https://youtu.be/Vpg9yizPP_g
http://youtu.be/Vpg9yizPP_g bla
[embed]https://youtu.be/Vpg9yizPP_g[/embed]
[embed] https://youtu.be/Vpg9yizPP_g [/embed]
[embed]http://youtu.be/Vpg9yizPP_g[/embed]
[embed] http://youtu.be/Vpg9yizPP_g [/embed]
<a href src=\"http://youtu.be/Vpg9yizPP_g\">vid</a>
<a href src=\"http://youtube.com/watch?feature=player_embedded&v=Vpg9yizPP_g\">vid</a>
<a href src=\"https://youtube.com/watch?v=Vpg9yizPP_g\">vid</a>";
let substitution = "\\1\\2\\3\\4";
// result will be a String with the substituted value
let result = regex.replace_all(string, substitution);
println!("{}", result);
}
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/