1 unstable release
Uses old Rust 2015
0.1.0 | Dec 23, 2016 |
---|
#69 in #regular
15KB
371 lines
regex-decode
A Rust library for extracting regex captures into a struct.
Usage
Add this to your Cargo.toml
:
[dependencies]
regex-decode = "0.1"
and this to your crate root:
extern crate regex_decode;
Here is a simple example that extracts named captures into a struct.
extern crate regex;
extern crate regex_decode;
extern crate rustc_serialize;
use regex::Regex;
use regex_decode::decode;
#[derive(RustcDecodable)]
struct Capture {
pub title: String,
pub year: usize,
}
fn test() {
let re = Regex::new(r"'(?P<title>[^']+)'\s+\((?P<year>\d{4})\)")
.unwrap();
let text = "Not my favorite movie: 'Citizen Kane' (1941).";
let val = decode::<Capture>(&re, &text).unwrap();
assert_eq!(&val.title, "Citizen Kane");
assert_eq!(val.year, 1941);
}
You can also extract to a tuple if you don't want to create a named struct.
extern crate regex;
extern crate regex_decode;
extern crate rustc_serialize;
use regex::Regex;
use regex_decode::decode;
fn test() {
let re = Regex::new(r"'(?P<title>[^']+)'\s+\((?P<year>\d{4})\)")
.unwrap();
let text = "Not my favorite movie: 'Citizen Kane' (1941).";
let (title, year) = decode::<(String, usize)>(&re, &text).unwrap();
assert_eq!(&title, "Citizen Kane");
assert_eq!(year, 1941);
}
Dependencies
~6–11MB
~139K SLoC