No description, website, or topics provided.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
src
.gitignore
.travis.yml
Cargo.toml
LICENSE
README.md

README.md

regex-decode

A Rust library for extracting regex captures into a struct.

Build Status

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);
}