Skip to main content

Regex Sample String Generator

Generate random strings that actually match a JavaScript regular expression, plus near-miss counterexamples that almost match but don't — useful for test fixtures, validating a pattern, or just seeing what it accepts.

Input

Written without the surrounding slashes — e.g. \d+, not /\d+/.

Bounds how large an unbounded repetition is allowed to grow when generating a sample.

Same seed + same settings always produces the same samples.

Output

Matching Samples
#Sample
No data yet
Near-Miss Counterexamples
#Near-missHow it differs from a match
No data yet
Was this helpful?

Guides

The Regex Sample String Generator does the opposite of a regex tester: instead of checking whether a pattern matches text you already have, it generates brand-new text that matches the pattern you give it. Paste a regular expression, click Generate samples, and get back a handful of strings that actually satisfy it — useful for test fixtures, seeding a form with realistic-looking data, or just sanity-checking that a pattern accepts what you think it does.

How it works

Enter a pattern (without the surrounding slashes — write \d+, not /\d+/). The tool parses it into its underlying structure and walks it: at every branch point — which side of an a|b alternation, how many times a +/*/{n,m} repeats, which character a [a-z0-9] class resolves to — it picks a random concrete value. Every candidate string is then checked against a real RegExp.test() call using your exact pattern and flags before it's shown to you, so what you get back is guaranteed to match, not just a plausible-looking guess.

  • Number of samples — how many matching strings to generate (1–20).
  • Max repeats — caps how large an open-ended repetition (*, +, {2,}) is allowed to grow. Lower it for shorter samples, raise it for longer ones.
  • Near-miss counterexamples — optionally also shows strings that almost match but don't (one character removed, added, or swapped), so you can see how forgiving or strict the pattern actually is.
  • Seed — leave blank for a fresh random draw every time, or set any text to make the same settings always produce the same output (handy for a reproducible test fixture).

How to use it

  1. Enter your pattern in Regular Expression Pattern.
  2. Toggle the flag checkboxes (i/m/s/u) to match how you'll actually use the regex.
  3. Adjust Number of samples and Max repeats if you want shorter/longer or more/fewer results.
  4. Click Generate samples. The Matching Samples table lists each result; the Near-Miss Counterexamples table (if enabled) shows strings that fail, and how they differ from a real match.
  5. Copy or download either table as CSV.

Why doesn't it just always produce exactly the number I asked for?

Some patterns are hard to satisfy by random generation alone — heavy use of lookaheads/lookbehinds or backreferences can make most random attempts fail verification. The tool retries a bounded number of times per sample and reports how many it actually managed; if it falls short, try a smaller Max repeats or simplify the pattern.

Does this run on your servers?

No — generation happens entirely in your browser using the same JavaScript regex engine as any other code in the page. Your pattern is never sent anywhere unless you explicitly use the API.

I already have text and want to check it against a pattern

Use the Regex Tester instead — it matches a pattern against text you supply and shows every match's position and capture groups. If you just want a pattern explained in plain English, try the Regex Explainer.

regexptest datafixture datamock datafuzz testingpattern matchrandexpregex101

More ways to use this tool

REST API

curl -X POST https://api.iotools.cloud/v1/tool/regex-sample-generator \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "pattern": "^\\d{5}-\\d{4}$",
    "count": "3",
    "maxRepeat": "6",
    "includeCounterexamples": "true",
    "seed": "iotools"
  }'

Swap in your own key from your account. The tool's fields are the body — no wrapper.

Ask an AI agent

Use the IOTools `regex-sample-generator` tool (Regex Sample String Generator) on this input:

YOUR_INPUT_HERE

Paste this at any agent connected to the IOTools MCP server, then add your input.

Embed widget

<iframe
  src="https://iotools.cloud/embed/regex-sample-generator/"
  width="100%" height="520" frameborder="0" scrolling="no" loading="lazy"
  title="Regex Sample String Generator — iotools.cloud"
  sandbox="allow-scripts allow-forms allow-same-origin allow-downloads allow-popups allow-popups-to-escape-sandbox"
  allow="clipboard-write"
  style="width:100%;border:1px solid #e5e7eb;border-radius:12px;overflow:hidden"></iframe>
<script src="https://iotools.cloud/embed.js" async></script>

Drop this into your own page — free, no key required, just a link back.

Cost per callFrom 5 credits

Also available with

Love the tools? Lose the ads.

One payment clears every ad from your account, for good. No subscription, no tracking.