Crate verex [−] [src]
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.
Examples
A simple example to show the usage:
use verex::Verex; use verex::find; // 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 funcitons 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")); // Test the generated regex strings assert_eq!(regex1.as_str(), r"(?:(?:a))"); assert_eq!(regex2.as_str(), r"(?:(?:a)|(?:b))"); assert_eq!(regex3.as_str(), r"(?:(?:a)|(?:b))");
Here's a URL testing example shamelessly stolen from the python Verex readme:
use verex::start_of_line; // 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() .clone(); 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\.)?(?:[^ ]*)$)");
Example usage of the or! macro:
#[macro_use(or)] extern crate verex; let regex = or!("foo", "bar", "baz") .compile() .unwrap(); // Test if the regex matches correctly assert!(regex.is_match("foo")); assert!(regex.is_match("bar")); assert!(regex.is_match("baz")); assert!(!regex.is_match("bum")); // Test the generated regex string assert_eq!(regex.as_str(), r"(?:(?:foo)|(?:bar)|(?:baz))");
Example usage of the or_expr! macro:
#[macro_use(or_expr)] extern crate verex; extern crate regex; use verex::Expression as E; use verex::Verex; use regex::Regex; let sub_verex = Verex::from_str("Darth(Vader)*?"); let sub_regex = Regex::new("(?P<robot>C3PO)").unwrap(); let regex = or_expr!( E::String("([RD]2){2}"), E::Verex(&sub_verex), E::Regex(&sub_regex)) .compile() .unwrap(); // Test if the regex matches correctly assert!(regex.is_match("R2D2")); assert!(regex.is_match("Darth")); assert!(regex.is_match("C3PO")); assert!(!regex.is_match("Anakin")); // Test the generated regex string assert_eq!(regex.as_str(), r"(?:(?:([RD]2){2})|(?:(?:Darth(Vader)*?))|(?:(?P<robot>C3PO)))");
Macros
| or! |
Match any of the given values |
| or_expr! |
Match any of the given sub-expressions |
Structs
| Verex |
The struct used for building verbal expression objects |
Enums
| Expression |
The enum used to have common functions for different Types |
Functions
| any |
Any of the given characters |
| any_of |
See any() |
| anything |
Any character zero or more times |
| anything_but |
Any character zero or more times except the provided characters |
| br |
A line break! |
| capture |
Find a specific string and capture it (will get escaped) |
| capture_expr |
Find an expression and capture it |
| digit |
Add the token for matching digits |
| end_of_line |
Add a token for the end of a line |
| find |
Find a specific string |
| find_expr |
Find an expression |
| line_break |
A line break! |
| maybe |
Any string either one or zero times |
| maybe_expr |
Any string either one or zero times |
| range |
A range of characters e.g. [A-Z] Usage example: verex.range(vec![('a', 'z'),('A', 'Z')]) |
| search_one_line |
Toggle whether ^ and $ match line start and end or string start and end |
| something |
Any character at least one time |
| something_but |
Any character at least one time except for these characters |
| start_of_line |
Add a token for the start of a line |
| tab |
Add a token for a tab |
| with_any_case |
Toggle whether to match case-sensitively or not |
| word |
Any alphanumeric characters |