What is a regex sample generator?
A regex sample generator produces example strings that match a regular expression. Give it the pattern [A-Z]{2}\d{3} and it returns something like "QK482" — two capital letters followed by three digits. The default pattern here produces a sample of 5 characters, and the same sample number always gives the same string.
Test data is the reason. A form that accepts a code, a username, or a reference number needs test values that match the pattern, and writing them by hand is slow and unrepresentative. A generator produces them in bulk, and each one is guaranteed to match — which is the point of testing against the pattern rather than against a guessed example.
This tool is also a way to understand a pattern. Reading a sample shows what the pattern actually accepts, which is often different from what its author intended. A pattern that was meant to accept codes such as "AB123" but also accepts "ZZ999" is working as written, and the sample makes that visible.
How sample generation works
The generator walks the pattern token by token and picks one value for each. A character class such as [A-Z] gives a random capital letter; \d gives a random digit; a literal character gives itself. A quantifier says how many times to repeat: {2} means exactly twice, {3} means exactly three times, * means zero to three times, and + means one to three times.
Groups and alternation are handled too. A group such as (ab|cd) picks one of its alternatives at random, and a group with a quantifier repeats the chosen alternative. Anchors ^ and $ produce nothing, because they mark positions rather than characters — the sample is the characters between them.
The sample number seeds the generator, so the same number always gives the same sample. That matters for tests: a test case that uses sample number 7 can be repeated exactly, and a colleague with the same pattern and the same number gets the same string. Change the number and you get a different sample from the same pattern.
Classes, quantifiers, and groups, explained
A character class is a set to pick from. [A-Z] picks one of 26 capital letters, [0-9] picks one of 10 digits, and [a-zA-Z] picks one of 52 letters. A negated class such as [^0-9] picks a character that is not a digit, which the generator does by choosing from letters and digits outside the excluded set.
A quantifier controls repetition. {2} repeats exactly twice, {2,4} repeats between two and four times, * repeats zero to three times, + repeats one to three times, and ? repeats zero or one time. The ranges for * and + are capped at three, so a sample never runs away — a pattern of \d* cannot produce a thousand digits.
Groups choose and repeat. (ab|cd) picks ab or cd, and (ab){2} repeats the chosen alternative twice. A non-capturing group (?:ab|cd) works the same way for generation. The generator handles one level of nesting inside a group, which covers the patterns people actually write for test data.
Quantifiers are capped in the generator, and the cap is a design choice rather than a limit of the pattern. A star means zero or more, which in theory could be any number, and a generator that honoured that literally would produce a string of unbounded length. The generator caps a star or a plus at three repeats, which is enough to show the shape of a match without producing a string that fills the screen. A pattern that needs a longer sample can be run again with a different seed, which gives a different length within the same cap.
A worked example: generating codes
Take the default pattern, [A-Z]{2}\d{3}, which is 13 characters. It means two capital letters followed by three digits. The generator picks two letters from A to Z and three digits from 0 to 9, giving a sample of 5 characters — a code in the shape of "AB123".
Change the sample number from 7 to 8 and the letters and digits change, but the shape stays the same: still two capitals and three digits. That is the contract of the tool — the shape is fixed by the pattern, and only the values vary. Generate ten samples and you have ten test codes, all valid.
Try a pattern with a choice: (cat|dog)\d gives either "cat" or "dog" followed by one digit, such as "dog7". Try a pattern with an optional part: colou?r gives "color" or "colour". The sample shows which branch was taken, which is how you check that both branches work.
Who uses a regex sample generator, and why
Testers are the main users. A form field with a validation pattern needs test values that pass and test values that fail, and the generator produces the passing ones in bulk. Ten samples from one pattern give ten test cases without typing.
Developers are the second group. A function that parses codes, references, or identifiers needs example inputs, and a generator produces them from the pattern the function already uses. The samples are guaranteed to match, so a failing test means the function is wrong, not the data.
Learners are a third group. A pattern is abstract until you see what it matches, and a sample makes it concrete. A case converter normalises the text first, so a pattern written for lower-case letters matches text that arrived in mixed case; this tool shows what the pattern produces.
Test engineers are a third group. A test that checks a validator needs both valid and invalid inputs, and the valid ones are the hardest to write by hand, because a valid postcode or a valid product code must follow a shape exactly. The generator produces valid inputs on demand: give it the pattern the validator uses, and every sample it makes is a valid input for the test. The invalid inputs are still written by hand, because the generator only makes strings that match.
Sample generator vs explainer and tester
A sample generator shows what a pattern produces; a tester shows what it matches in your text. The regex tester on this site highlights every match of a pattern in your text, which answers "does this pattern match this text?" This tool answers "what does a match look like?"
The regex tester answers a third question: "does this pattern match this text?" Paste a sample into the tester with the same pattern and it must match — which is a good cross-check. If the sample does not match, either the generator or the pattern is wrong, and the tester tells you which.
Use them in order: explain the pattern to understand it, generate samples to see its shape, and test the samples against real text to check the edges. A pattern that passes all three checks is one you can trust.
A sample generator is not a tester, and the distinction matters when the question is whether a string matches. A generator makes strings that match the pattern, which answers what a match looks like. A tester checks whether a given string matches, which answers whether a particular string is valid. Use the generator to see the shape and the tester to check a real string — a sample that looks right in the generator can still fail in the tester if the pattern is anchored and the string has extra characters.
Common sample generation mistakes
The first mistake is assuming a sample proves the pattern is correct. A sample shows one match, not all matches. A pattern of [A-Z]{2}\d{3} also accepts "ZZ999", which may not be a valid code in your system — the pattern describes a shape, and the shape may be broader than the real rule.
Second: forgetting that * can produce nothing. A pattern of ab*c can generate "ac", with zero b's, which is a valid match but a surprising test case. If your data always has at least one of something, use + instead of *.
Third: expecting filters or lookaheads. This generator supports the common constructs — classes, escapes, quantifiers, groups, and alternation — but not lookaheads or backreferences, which have no single sample to generate. A pattern using them is reported as unsupported for the affected part, not silently mis-generated.
Fourth: using random samples as real data. Generated codes are test data, not real identifiers. Never use a generated string as a real order number, a real password, or a real key — it was made to match a shape, not to be unique or secure. For a real password, use a password generator, which is built for that job.
A second mistake is using a generated sample as real data. A generated code looks like a real order number, a real postcode, or a real password, but it was made to match a shape, not to be unique or secure. Two runs with the same seed produce the same sample, and two runs with different seeds can produce the same sample by chance, because the generator only knows the shape. Never use a generated string as a real identifier, a real password, or a real key — it is test data, and test data belongs in tests.
How to use this regex sample generator
Type a regular expression into the box — the default is [A-Z]{2}\d{3} — and pick a sample number. The default is 7. Press Generate Sample, and a string matching the pattern appears in the box below, 5 characters long for the default pattern.
To get another sample, change the number and press generate again. To copy the sample, use the Copy button next to the output box. To check the sample, paste it into the regex tester with the same pattern — it must match.
Everything runs in your browser. No sign-up, no account, no data sent anywhere — your pattern never leaves your device. It works offline once the page has loaded. Generate ten samples for a test case, and keep the sample numbers so the tests can be repeated exactly.
Type the pattern, pick a seed, and press generate. The sample appears with the pattern beside it, so the shape is easy to check — a pattern for a code of three letters and four digits should produce exactly that, and a sample that is longer or shorter means the pattern is not what you thought. Change the seed to see a different sample from the same pattern, which is the quickest way to learn what the pattern allows. When the samples all match the shape you intended, the pattern is ready to test against real text.