// 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"(?imU)(http\:|ftp\:|https\:)?(\/\/)?([\w_-]+(?:(?:\.[\w_-]+)+)[\w.,@?^=%&:\/~+#-()].*[\w@?^=%&\/~+#-]?)(?=\s|$)").unwrap();
let string = "https://en.wikipedia.org/wiki/Salt_(2010_film)
https://en.wikipedia.org/wiki/Salt_2010_film
https://en.wikipedia.org/wiki/Salt-2010-film
HTTPS://en.wikipedia.org/wiki/Salt-2010-film
//en.wikipedia.org/wiki/Salt_2010_film
https://en.wikipedia.org/wiki/Salt_2010_film?cat=1
https://en.wikipedia.org/wiki/Salt_2010_film#anchor
https://en.wikipedia.org/wiki/Salt_(2010_film)?cat=2
This is a sentence that has a link in it. https://en.wikipedia.org/wiki/Salt_(2010_film)?cat=3 This regex allows me to create an HTML anchor tag around it.";
let substitution = "<a href=\"${1}${2}${3}\" target=\"_blank\">${1}${2}${3}</a>";
// 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/