ethanpailes / remake

Apache License 2.0
4 stars 1 forks source link

remake

A rust library for writing maintainable regex. Regex are wonderful things, but they lack tools for abstraction and modularity. When regex are small, this is not a big issue, but as they grow larger it can become frustrating to maintain them. Remake allows you to name individual regex and combine them just like you would combine strings or numbers in a traditional programming language. When you want to use the terse regex syntax that you know and love, you still can, but when you want to break things down into bite sized pieces it is easy.

Build Status Docs

Documentation

The module docs contain a full explanation of the remake language with inline examples.

Usage

Add this to your Cargo.toml:

[dependencies]
remake = "0.1.0"

and this to your crate root:

extern crate remake;

Here is a simple example that builds a toy URI validator

extern crate remake;

use remake::Remake;

fn main() {
    let web_uri_re = Remake::compile(r#"
        let scheme = /https?:/ . '//';
        let auth = /[\w\.\-_]+/;
        let path = ('/' . /[\w\-_]+/)*;
        let query_body = (/[\w\.\-_?]/ | '/')*;
        let frag_body = cap query_body as frag;

          /^/
        . scheme . auth . path
        . ('?' . query_body)?
        . ('#' . frag_body)?
        . /$/
        "#).unwrap();

    assert!(web_uri_re.is_match("https://www.rust-lang.org"));
    assert!(web_uri_re.is_match("https://github.com/ethanpailes/remake"));

    assert_eq!(
        web_uri_re
            .captures("https://tools.ietf.org/html/rfc3986#section-1.1.3")
                .unwrap().name("frag").unwrap().as_str(),
            "section-1.1.3");
}