10 releases (6 breaking)

0.17.0 Jan 15, 2024
0.16.0 Aug 4, 2023
0.15.1 Feb 12, 2021
0.14.2 Feb 1, 2020
0.12.0 Nov 25, 2018

#56 in Algorithms

Download history 7780/week @ 2023-11-24 6564/week @ 2023-12-01 6952/week @ 2023-12-08 8070/week @ 2023-12-15 3407/week @ 2023-12-22 5369/week @ 2023-12-29 6596/week @ 2024-01-05 6860/week @ 2024-01-12 8822/week @ 2024-01-19 6844/week @ 2024-01-26 7709/week @ 2024-02-02 9239/week @ 2024-02-09 8504/week @ 2024-02-16 9309/week @ 2024-02-23 8646/week @ 2024-03-01 4452/week @ 2024-03-08

32,650 downloads per month
Used in 45 crates (12 directly)

MIT license

48KB
870 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.4MB
~38K SLoC