// 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"(?mi)^(?!.*::.*::)fe80(?=(?::+[^:]+){0,6}$)(?:(:0{1,4})*|(?1){3,}(?::+[^:]+)):(?=:)(:[0-9a-f]{1,4}){0,4}$|^fe80(?=(?::[^:]+){7}$)(?1){3,}(?2){0,4}$").unwrap();
let string = "valid cases:
FE80::1
FE80::1:0:1
FE80::0000:1
FE80:0::1
FE80:000::1
FE80:0:00::1
FE80:0:00:000::1
FE80:0:00:000:0000::1
fe80::9656:d028:8652:66b6
fe80:0000:0000:0000:0000:0000:0000:0000
fe80:00:000:0000:1234:5678:ABCD:EF
fe80:00:000:0000:1234:5678:ABCD:00
fe80::1234:5678:ABCD:EF
fe80:00:000:0000::EF
fe80:00::ABCD:EF
FE80:0:00:000:0001::1
FE80:0:00:000:0000:1::1
invalid cases:
fe80:66b6:9656:d028:8652:66b6:9656:d028:0000
fe80:66b6:9656:d028:8652:66b6:9656:d028
fe80:1:000:0000:1234:5678:ABCD:EF:9999
fe80::bad:1234:5678:ABCD:EF
fe80:0:bad::EF
fe80:1:2:3:4:6:5:7
fe80:0:bad:EF
Fe90::1
2000::1
fe80:00:000:0000:0:0:0::EF
fe80:0:0:0:ABCD:EF:0
FE80:0:00:001:0001::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/