Generate a regex from a few sample strings instead of writing it by hand
Can I generate a regex from a few sample strings instead of writing it by hand?
Yes. Drop a few example strings into a regex generator and it will work out the shared structure and hand you a pattern you can test. The catch: you also want to feed it strings that should not match. Skip that and the tool tends to return something accidentally too broad.
It's guessing, not magic. Treat the output as a starting point. Anchors, character classes, edge cases: you still own those.
How does sample-based regex generation actually work?

The generator tries to find the smallest pattern that keeps the good examples and rejects the bad ones. So the samples you pick really shape the result. Mix short and long strings, uppercase and lowercase, numbers and punctuation.
Say you paste INV-1234, INV-5678, INV-9012. It spots the fixed INV- prefix and the four digits, and comes back with something like ^INV-\d{4}$. But with only those three inputs it has no way to know INV-123 should fail. Add a couple of negatives and the pattern tightens noticeably.
What should my example strings look like?
Positives first, the strings that must pass. Then negatives, the ones that must fail. For INV-1234 and INV-0001, decent negatives are INV-123, ORDER-1234, and inv-1234. The generator uses those to narrow things down, so it won't accept order numbers or lowercase variants.
Edge cases too: empty string, stray spaces, odd punctuation, very long values. A pattern that passes your three samples can still trip on real input.
Where do I try this?
regextoolbox.com works well. It's browser-based, so you can paste samples, get a pattern, test it against new strings, and tweak as you go. No need to memorize regex syntax. It's also fine for sanity-checking a pattern you already wrote by hand.
Why does the generated pattern look wrong?
A few samples can fit several patterns, and the generator picks one, usually the simplest. That's where the trouble starts. Watch for these:
- Missing anchors. No
^and$, and the pattern matches anywhere in a string. - Wrong character classes.
\wis not "digits only". - No length limit.
\d+will happily acceptINV-1234567890. - Unescaped special characters.
.,+,?, and*have regex meanings, so escape them when you want them literally.
How do I test the generated regex before using it?
Run it against the positives and negatives, then throw in the edge cases. Empty string, extra spaces, punctuation, shorter and longer values. Then test in the same language or tool where the regex will actually run. Python, JavaScript, and grep don't share one regex flavor; a pattern that works in one can behave differently in another.
Is sample-based generation good enough for production code?
It's a fast first draft, and that's about it. Handy for IDs, product codes, log entries, and structured text. Review the logic, test the realistic edge cases, refine until it matches your real requirements. If the validation genuinely can't fail, write the pattern by hand and understand every part of it.