5 releases

Uses old Rust 2015

0.2.3 Mar 7, 2023
0.2.2 Mar 7, 2023
0.2.1 Aug 27, 2019
0.2.0 May 31, 2017
0.1.0 May 30, 2017

#307 in Text processing

Download history 115/week @ 2023-12-05 246/week @ 2023-12-12 97/week @ 2023-12-19 52/week @ 2023-12-26 178/week @ 2024-01-02 264/week @ 2024-01-09 221/week @ 2024-01-16 456/week @ 2024-01-23 525/week @ 2024-01-30 639/week @ 2024-02-06 276/week @ 2024-02-13 292/week @ 2024-02-20 599/week @ 2024-02-27 365/week @ 2024-03-05 361/week @ 2024-03-12 255/week @ 2024-03-19

1,632 downloads per month
Used in 4 crates

MIT/Apache

17KB
345 lines

regex_generate

Use regular expressions to generate text. This crate is very new and raw. It's a work-in-progress, but feel free to add issues or PRs or use it for your own ideas, if you find it interesting. No guarantees or warranties are implied, use this code at your own risk.

Thanks to the amazing folks who work on rust-lang/regex which is the heart of this crate. Using regex_syntax made this crate 1000x easier to produce.

Documentation

Magically generated and graciously hosted by Docs.rs.

The documentation is not good right now.

Usage

Add this to your Cargo.toml:

[dependencies]
regex_generate = "0.2"

and this to your crate root:

extern crate regex_generate;

This example generates a date in YYYY-MM-DD format and prints it. Adapted from the example for rust-lang/regex.

extern crate regex_generate;
extern crate rand;

use regex_generate::{DEFAULT_MAX_REPEAT, Generator};

fn main() {
    let mut gen = Generator::new(r"(?x)
(?P<year>[0-9]{4})  # the year
-
(?P<month>[0-9]{2}) # the month
-
(?P<day>[0-9]{2})   # the day
", rand::thread_rng(), DEFAULT_MAX_REPEAT).unwrap();
    let mut buffer = vec![];
    gen.generate(&mut buffer).unwrap();
    let output = String::from_utf8(buffer).unwrap();

    println!("Random Date: {}", output);
}

Tests

Run tests with cargo test -- --nocapture

Benches

Run benchmarks with rustup run nightly cargo bench

Tips

  • Be explicit in your character classes or you will get unexpected results.
  • . really means any, as in any valid unicode character.
  • Likewise, \d means any number, not just [0-9].
  • The default maximum for repetitions (like .*) is 100, but you can set it yourself with generate_with_max_repeat.

TODO

  • Add convenience method for directly generating complete strings
  • Implement Iter for making lots of strings?
  • Add tests for regex bytes feature
  • Account for case insensitivity in Literal
  • Do something with group numbers or names? (No back referencing in the syntax, so maybe nothing can be done.)

License

regex_generate is primarily distributed under the terms of both the MIT license and the Apache License (Version 2.0), with portions covered by various BSD-like licenses.

See LICENSE-APACHE and LICENSE-MIT for details.

Dependencies

~5.5MB
~126K SLoC