#regex #random #generator

rand_regex

Generates random strings and byte strings matching a regex

8 releases (4 breaking)

0.15.1 Feb 12, 2021
0.15.0 Jan 9, 2021
0.14.2 Feb 1, 2020
0.14.1 Jan 27, 2020
0.11.0 Oct 27, 2018

#280 in Text processing

Download history 3014/week @ 2022-12-01 3521/week @ 2022-12-08 4275/week @ 2022-12-15 1873/week @ 2022-12-22 1673/week @ 2022-12-29 2995/week @ 2023-01-05 2902/week @ 2023-01-12 2930/week @ 2023-01-19 3164/week @ 2023-01-26 3859/week @ 2023-02-02 3467/week @ 2023-02-09 3127/week @ 2023-02-16 3837/week @ 2023-02-23 3326/week @ 2023-03-02 4520/week @ 2023-03-09 4438/week @ 2023-03-16

16,741 downloads per month
Used in 32 crates (7 directly)

MIT license

44KB
859 lines

rand_regex

Crates.io Build status MIT License

Generates random strings and byte strings matching a regex.

Examples

use rand::{SeedableRng, Rng};

let mut rng = rand_xorshift::XorShiftRng::from_seed(*b"The initial seed");

// creates a generator for sampling strings
let gen = rand_regex::Regex::compile(r"\d{4}-\d{2}-\d{2}", 100).unwrap();

// sample a few strings randomly
let samples = (&mut rng).sample_iter(&gen).take(3).collect::<Vec<String>>();

// all Unicode characters are included when sampling
assert_eq!(samples, vec![
    "᱃៧७᧗-꤂႔-૪۰".to_string(),
    "𝟽٩𑃶᱒-៤꣖-൭᧓".to_string(),
    "𑃰꩗१௭-9၅-६௫".to_string(),
]);

// you could use `regex_syntax::Hir` to include more options
let mut parser = regex_syntax::ParserBuilder::new().unicode(false).build();
let hir = parser.parse(r"\d{4}-\d{2}-\d{2}").unwrap();
let gen = rand_regex::Regex::with_hir(hir, 100).unwrap();
let samples = (&mut rng).sample_iter(&gen).take(3).collect::<Vec<String>>();
assert_eq!(samples, vec![
    "8922-87-63".to_string(),
    "3149-18-88".to_string(),
    "5420-58-55".to_string(),
]);

Acknowledgement

rand_regex is heavily inspired by regex_generate and proptest.

Dependencies

~1–1.3MB
~37K SLoC