Skip to content

Instantly share code, notes, and snippets.


ciju/ Secret

Last active Feb 28, 2021
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

Few more regex examples:

> /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{8}/

> /.{8,12}/

> /[^aeiouAEIOU0-9]{5}/

> /[a-f-]{5}/


  1. Please don't use external libraries. Especially, write the tokenizer/parser yourself. If you use tokenizer/parsers shipped with languages (go, and python seem to have regular expression parsers), your solution would be rejected.
  2. Generate possibilities randomly. Sequentially listing through the possibilities might not be as elegant/useful.
  3. Please strive for simplicity and clarity. Write comments and instructions on how to run, etc. If your solution makes reviewing difficult, reviewer might reject it.


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