// 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#"(?ms)"(?:[^"]|"")*"|[^,\n]+|(?=,)(?<=,)|^|(?<=,)$"#).unwrap();
let string = "record 1:field 1,record 1:field 2,record 1:field 3
record 2:field 1,record 2:field 2,record 2:field 3
\"quoted field\",\"quoted field, with comma\", \"whitespace before & affer quote\" ,\"new
line in field\",\"double quotes \"\"in\"\" field\",\"double quotes in field \"\",\"\" with comma\"
,,,<- empty fields at beging, , ,<-whitespace fields,empty fields at end ->,,
^empty records";
// 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/