// 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"(?i)(?=\S)(?<text>(?<comment>(?<open>[#;]+)(?:[^\S\r\n]*)(?<value>.+))|(?<section>(?<open>\[)(?:\s*)(?<value>[^\]]*\S+)(?:[^\S\r\n]*)(?<close>\]))|(?<entry>(?<key>[^=\r\n\[\]]*\S)(?:[^\S\r\n]*)(?<delimiter>:|=)(?:[^\S\r\n]*)(?<value>[^#;\r\n]*))|(?<undefined>.+))(?<=\S)|(?<linebreaker>\r\n|\n)|(?<whitespace>[^\S\r\n]+)").unwrap();
let string = "; Comment above the section
Key0=Value0
[Section 1]
Key=Value
[EmptySection]
Dummy text
;Commented Key=Value
;The following empty lines are ignored
;
#
[ Section 2 ]#Comment after [section];
Key 1 = Value 1
Key2 = Value 2 and [text in square brackets]
EmptyKey=
[];Noname section ignored
[ Section2 ] Dummy text after section
Key3 =Value 3 (symbols [;] are included)
#Key4 = Value 4 has been commented
Dummy text before section [ Sectionnum3 ]
Key 5 = Value 5
Key6 =Value6 ";
// 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/