GenerateRandomSearch

Regex String Generator

A validation rule is usually written as a regular expression, and then somebody has to invent twenty strings that satisfy it to test against. People write the same three shapes over and over and never produce the awkward ones the pattern also allows. This runs the pattern backwards instead — and then checks its own work with the real engine.

What this generator does

Parses the expression into a tree of literals, classes, groups, alternations and repeats, then walks it choosing among the options as it goes — and discards anything the real engine does not accept.

How to use this tool

  1. Type a regular expression, or press one of the examples.
  2. Choose how many strings you want.
  3. Add a seed if you need the same set again for a repeatable test.
  4. Copy the strings straight into a test or a fixture file.

Understanding the controls

The pattern
The regular expression, written as you would write it in code. Literals, character classes and ranges, the shorthands for digits, word characters and whitespace, the dot, groups, alternation, and the repeats *, +, ?, {n} and {n,m} are all understood.
How many strings
Up to fifty at once. Where the pattern has fewer distinct strings than that, you get all of them and no repeats: a pattern that small is written out in full, so nothing the random walk rarely reaches is left out.
Seed (optional)
Type anything to get the same strings again, which is what a repeatable test needs. Deterministic, not cryptographic.

Worked examples

[A-Z]{2}\d{4}, seeded with "demo"
DB2809, VM6830, RK3925, UZ0508, XF5754 — five of the 6,760,000 distinct strings this pattern can produce (26 x 26 x 10,000), each independently tested against the pattern by the browser's own regex engine before being shown.

Common use cases

  • Building test data for a field with a validation rule
  • Checking that a pattern really allows what you think it does
  • Seeing the awkward shapes a pattern permits that you would not have written
  • Producing sample reference numbers, codes or identifiers in a house format
  • Teaching regular expressions by showing what a pattern generates rather than what it rejects

How this generator works

The expression is parsed into a tree and walked, choosing at random among the branches of an alternation and among the counts a repeat allows. A repeat with no upper bound has to stop somewhere: * and + stop at eight, and {n,} at eight more than n. Then comes the check that matters: every string is tested against the pattern anchored at both ends, using the browser's own regular expression engine rather than the parser that produced it — a generator that agrees only with itself proves nothing. The page also counts how many distinct strings the pattern can produce, where that is a finite number, so you can see when you have asked for more than exist: up to two thousand it writes every one out and the count is exact, and beyond that it counts the routes through the pattern, which is an upper limit.

Randomness and fairness

The choices come from your browser's cryptographic random source unless you give a seed, in which case the same seed gives the same strings — deterministic, not cryptographic.

For how randomness is produced across the whole site, see how Generate Random works.

Limitations and good to know

  • Lookarounds, backreferences and word boundaries cannot be run backwards and are refused with a message saying so.
  • An unbounded repeat is capped — * and + at eight, {n,} at eight beyond n — because otherwise there is no longest string.
  • The dot produces printable characters rather than the whole of Unicode.
  • Negated classes are drawn from the same printable set, so they do not produce control characters.

Privacy and your data

The pattern is parsed and the strings produced in your browser. Neither the pattern nor the results are transmitted or stored.

Frequently asked questions

How does this generate a string from a pattern instead of just matching one?
It parses the expression into the same tree structure a regex engine builds internally — literals, character classes, groups, alternation, repeats — then walks that tree making a random choice at each branch (which alternative, how many repeats) instead of checking a fixed string against it. It's matching run in reverse: building a string that satisfies the pattern rather than testing whether one already does.
Can it generate strings for any pattern I write?
Not lookarounds, backreferences or word boundaries — those describe a relationship to surrounding text or an earlier match rather than a self-contained shape, so there's no way to build a standalone string from them. The page tells you plainly when a pattern uses one of these instead of silently producing something wrong.