Giter Club home page Giter Club logo

readable-regex's Introduction

Readable regex

MIT licensed Crates.io GHA Build Status Docs Badge

Regex for human beings.

Why?

Regex is useful. But, sincerely, since code it is more read than written regex could be more understandable in a verbose mode.

readable-regex crate is a set of tools to build those regexes in a verbose way. Which aims to improve readability of code.

It builds on top on the already excellent regex and fancy-regex crates.

Add dependency

Add the dependency to your Cargo.toml file:

readable-regex = "0.1.0"

Usage

Available APIs

The main wrapper is the ReadableRe enum.

There are tow main options to build regexes with it, either using the enum itself:

use readable_regex::ReadableRe;
let query = ReadableRe::Raw("<some regex expression>");

or by using the functions wrappers around it:

use readable_regex::raw_regex;
let query = raw_regex("<some regex expression>");

Also, any combination of them:

use readable_regex::{digit, group};
use readable_regex::ReadableRe::*;
let query = group(digit() + Digit + digit());
println!("{}", query.to_string());

Example

How to build a simple date match (as implemented in the datetime module under presets feature):

use once_cell::sync::Lazy;
use readable_regex::*;
use readable_regex::ReadableRe::*;

/// Month day, `01`-`31`
pub const DAY: Lazy<ReadableRe> = Lazy::new(|| {
    either([
        Raw("0") + chars("1-9"),
        chars("12") + chars("1-9"),
        Raw("3") + chars("01"),
    ])
});

/// Month numeral, `01`-`12`
pub const MONTH: Lazy<ReadableRe> =
    Lazy::new(|| either([Raw("0") + chars("1-9"), Raw("1") + chars("0-2")]));

/// Years from `1000` to `2999`
pub const YEAR: Lazy<ReadableRe> = Lazy::new(|| chars("12") + exactly(3, Digit));

/// Date Format `YYYY-MM-dd`
pub const DATE_Y_M_D: Lazy<ReadableRe> = Lazy::new(|| {
    group(
        group(YEAR.clone())
            + chars(r"-/.\\")
            + group(MONTH.clone())
            + chars(r"-/.\\")
            + group(DAY.clone()),
    )
});

assert!(DATE_Y_M_D.compile().unwrap().is_match("2022/04/18"));

Acknowledges

This library was highly inspired by the python Humre package.

readable-regex's People

Contributors

danielsanchezq avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.