// 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)[-\[,:.\/]?\b(?:1\d{3}|200[0123]|\d0)(?:'?s)?\b[-\],:.\/]?").unwrap();
let string = "I need it to catch a date between 1000 and 2003 in these forms:
1975
1970s
1970's
1970S
1970'S
70s
70's
70S
70'S
I also need it to catch certain characters on either side of the date, including
dashes
-1973-
brackets
[1973]
commas
,1973,
colons
:1973:
periods
.1973.
slashes
/1973/
- some on both sides, some on only one.
/1973
or
1973/
My problem is that the expression is catching other characers on either side of the date as well - +1975 gets through, for instance, as does 1970s& - letters and numbers on either side do not get through, however. I'm confused.
I think I might need some sort of limit on either side before I can state the exceptions, I'm not sure what that would look like - some kind of look back? Any help would be appreciated.";
// 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/