// 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)^(?P<indentation>(?P<sp_or_tab>[ \t])*+)(?P<stmt>(?P<non_str_lit>(?:(?!\#)[^\"'\r\n])*?)(?:(?P<str_lit>(?P<begin_quote>(?P<single_quote>')|\")(?:(?(single_quote)\"|')|\\[\"']|\\[^\r\n ]|[^\"'\\\r\n])*?(?P=begin_quote))(?&non_str_lit))*?)(?P<whitespace>(?&sp_or_tab)*+)(?P<comment>(?:\#)[^\r\n]*+)?(?P<line_ending>\r|\n|\r\n)?$"#).unwrap();
let string = "print(\"Hello, world!\") # this is a comment.
print(\"The quick \\\\brown\\\\\\ndog\\rfox\\tjumps\\t over #the# /lazy/ \\\"dog\\\"###\") # and here's where the real comment starts
print(\"you can use Alt+0009 to enter a tab character!\")
# The above line had no comments, so the <comment> group didn't participate in the match.
print(\"indented code\") # with comment
print(\"never indent code like this, but it still matches anyway\")# comment lacking preceding whitespace >:)
print(\"space indented code\")
print(\"Missing end quote
print(\"unescaped \\\")
print(\"implicitly\" \" joined## \" \"string \" \"lit#erals\") # a line can contain multiple string literals
print(\"the quick \" # line 1
\"brown fox \" # line 2
\"jumps over \" # line 3
\"the lazy dog\") # line 4
print(\"The quick brown\", animal, \"jumps over the\", adjective, animal2, sep=\" \") \"
print('Robert'); DROP TABLE `Students`; -- ')";
// 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/