[−][src]Crate rec
Regular Expression Constructor - making regular expressions fun
rec
is a Rust library that simplifies the process of writing, reading, and using regular
expressions. This library is intended for all users working with regular expressions, no matter
their familiarity with regular expression syntax. Below is a summary of the functionality
provided by rec
:
- WYSIWYG: &str is interpreted exactly as written (i.e. no metacharacters); all metacharacters
(as well as other useful patterns) are provided by the
ChCls
enum. - Simple to understand quantifier syntax and capture name syntax.
- Uses operators to provide easy to understand expressions.
Pattern
returns exactly what is requested to reduce boilerplate.
This library utilizes the regex
crate.
Getting Started
Add the following to your Cargo.toml
:
[dependencies]
rec = "0.4"
Examples
Create a Regex
If you prefer API of regex
, you can use a Rec
to construct a Regex
.
use rec::{some}; use rec::ChCls::{Digit, Whitespace}; let a_rec = "hello" + some(Whitespace) + (Digit | "world"); let regex = a_rec.build(); assert!(regex.is_match("hello world"));
Use Pattern to tokenize
Instead of using Regex
, you can use Pattern
to handle basic matching needs.
use rec::{some, tkn, var, Element, Pattern}; use rec::ChCls::Digit; let decimal_number = Pattern::new(tkn!(some(Digit) => "whole") + "." + var(Digit)); assert_eq!(decimal_number.tokenize("23.2").get("whole"), Some("23"));
FAQ
I know regular expression syntax; why should I use rec
?
In order for code to be easily maintainable, it should be as simple as possible. Even if the original developer understands their regular expression, it is beneficial for the project as a whole if all contributors are able to easily understand the function of a regular expression.
Macros
tkn | Creates a capture group with a given name. |
Structs
Location | Represents where in the target that a match was found. |
Locations | Iterates through a target, returning the |
Pattern | Represents a regular expression to be matched against a target. |
Rec | Constructs a regular expression. |
Tokens | Stores the possible matches of a |
TokensIter | Iterates through a given target returning each |
Enums
ChCls | Specifies a set of characters to be matched against. |
Traits
Element | Signifies elements that can be converted into a |
Functions
btwn | Returns a |
exact | Returns a |
lazy_btwn | Returns a |
lazy_min | Returns a |
lazy_opt | Returns a |
lazy_some | Returns a |
lazy_var | Returns a |
min | Returns a |
opt | Returns a |
some | Returns a |
var | Returns a |