#regex #string #regex-builder

verex

This crate provides a Rust implementation of VerbalExpressions in order to build regex strings without knowing the minutiae of regex syntax

5 releases

Uses old Rust 2015

0.2.2 Feb 8, 2016
0.2.1 Jan 29, 2016
0.2.0 Jan 22, 2016
0.1.1 Jan 18, 2016
0.1.0 Jan 18, 2016

#422 in #regex

24 downloads per month

MIT license

39KB
926 lines

RustVerbalExpressions

This crate provides a Rust implementation of VerbalExpressions in order to build regex strings without knowing the minutiae of regex syntax. It uses the regex crate to compile the created regex strings.

Build Status

Versions are numbered according to semver.

Usage

Add this to your Cargo.toml:

[dependencies]
verex = "0.2"

and this to your crate root:

extern crate verex;

Examples

A simple example to show the usage:

extern crate verex;
use verex::Verex;
use verex::find;

fn main() {
    // You can either use a mutable Verex to define different regexes
    let mut verex = Verex::new();
    let regex1 = verex.find("a")
                      .compile()
                      .unwrap();
    let regex2 = verex.or_find("b")
                      .compile()
                      .unwrap();
    // Or just use it for building one (you can use the functions directly as constructors)
    let regex3 = find("a") // or: Verex::new().find("a")
                 .or_find("b")
                 .compile()
                 .unwrap();

    // Test whether the regexes match correctly
    assert!(!regex1.is_match("b"));
    assert!(regex2.is_match("b"));
    assert!(regex3.is_match("b"));
}

Here's a URL testing example shamelessly stolen from the python Verex readme:

extern crate verex;
use verex::start_of_line;

fn main() {
    // Create an example of how to test for correctly formed URLs
    let verex = start_of_line()
                .find("http")
                .maybe("s")
                .find("://")
                .maybe("www.")
                .anything_but(" ")
                .end_of_line();
    let regex = verex.compile().unwrap();
    // Create an example URL
    let test_url = r"https://www.google.com";
    // Test if the URL is valid
    assert!(regex.is_match(test_url));
    // Test the generated regex string
    assert_eq!(verex.source(), r"(?:^(?:http)(?:s)?(?:://)(?:www\.)?(?:[^ ]*)$)");
}

Dependencies

~4MB
~80K SLoC