Skip to content

Instantly share code, notes, and snippets.


ciju/ Secret

Last active Nov 21, 2020
What would you like to do?
Random String Generator

Random String Generator

Write a string generator that takes a regular expression as argument and returns strings that match the given regular expression.

generate(/[-+]?[0-9]{1,16}[.][0-9]{1,6}/, 10)

should return results like


The generator should be able to support following features:

. Match any character except newline
[ Start character class definition
] End character class definition
? 0 or 1 quantifier
* 0 or more quantifiers
+ 1 or more quantifier
{ Start min/max quantifier
} End min/max quantifier

Within a character class, the following meta characters should be supported:

^ Negate the class, but only if the first character
- Indicates character range


The following features are optional.

| Start of alternative branch
( Start subpattern
) End subpattern
\1 back reference

Adding support for these features would make the generator able to handle complex patterns like

generate(/(1[0-2]|0[1-9])(:[0-5][0-9]){2} (A|P)M/, 10)

"10:43:51 PM"
"10:41:31 PM"
"03:09:55 PM"
"11:19:50 AM"
"11:20:41 PM"
"01:15:54 PM"
"02:10:04 AM"
"03:43:47 PM"
"09:39:03 AM"
"11:23:46 PM"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.