Regex in Rust
In working on my .eml
parser library, EmlParser
, I decided using a regex would be way easier than trying to roll my own parser for handling RFC-0822’s definition for route-addr
. But I hadn’t yet used regular expressions in Rust, so voilà:
Cargo.toml
[dependencies]
regex = "1"
main.rs
use regex::Regex;
fn main() {
let email = r#""John Smith" <jsmith@example.com>"#;
let name_addr_re = Regex::new(r#""(.?+)" <([^>]+)>"#).unwrap();
match name_addr_re.captures(email) {
Some(cap) => {
let name = cap.get(1).unwrap();
let addr = cap.get(2).unwrap();
let namestr : &str = name.as_str();
println!("I found name = {}", name.as_str()); // "John Smith"
println!("I found addr = {}", addr.as_str()); // "jsmith@example.com"
},
None => println!("doh!?")
};
}
The hardest part seems to be figuring out the Rust API (regex
docs here) – .captures
gets you what’s analogous to Python’s Match.group
, etc.