// 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)^((http|https|ftp|ftps):\/\/)?(?<url_host>(\[[^\]]+\]|[^\/:\n]+))(?<url_port>(:\d+)?)(\/)?").unwrap();
let string = "sony.com
http://cnn.com/story
https://google.com:444/stuff
1.2.3.4:12/ok
1.2.3.4/ok
2.2.2.2:443
/dontmatchme
ftp://mononym/path
http://google.com
www.test
http://[2001:1890:1c00:6500::d:7]/rar/
[2001:1890:1c00:6500::d:7]
[2001:1890:1c00:6500::d:7]/test
http://[2001:1890:1c00:6500::d:7]:8080/rar/
[2001:1890:1c00:6500::d:7]:8080
[2001:1890:1c00:6500::d:7]:4/test
";
// 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/