rand_regex

Crates.iorand_regex
lib.rsrand_regex
version
sourcesrc
created_at2018-10-27 17:18:04.453152
updated_at2025-01-28 10:55:15.47033
descriptionGenerates random strings and byte strings matching a regex
homepage
repositoryhttps://github.com/kennytm/rand_regex
max_upload_size
id92957
Cargo.toml error:TOML parse error at line 19, column 1 | 19 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include`
size0
(kennytm)

documentation

README

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(),
    "𞓰۳𑛐꩑-᪄9-໔᮹".to_string(),
    "𑛃𑃹९೭-١᥈-৫೪".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![
    "2839-82-12".to_string(),
    "2857-86-63".to_string(),
    "0381-04-99".to_string(),
]);

Acknowledgement

rand_regex is heavily inspired by regex_generate and proptest.

Commit count: 30

cargo fmt