About this tool
Writing a regex from scratch means remembering syntax and guessing edge cases. This generator works from examples instead: list strings that should match, and it builds a pattern that describes them. Add strings that should not match, and every example is checked so you can see at a glance whether the pattern does what you want.
How the pattern is built
- Each example is split into runs of the same kind of character: uppercase letters, lowercase letters, digits, spaces, and individual symbols like
-,@or.. - If all examples share the same structure, each run becomes a character class with a length range: two to three capitals is
[A-Z]{2,3}, four digits\d{4}. - Parts that are identical in every example (like an
INV-prefix) stay as literal text. - If the structures differ, broader classes are tried (any letter, then letters and digits); if they still differ, the pattern lists the alternatives.
For example, INV-2024-0001 and INV-2023-0117 give ^INV-\d{4}-\d{4}$.
Get a better pattern
- Give varied examples: different lengths, letters and digits. Two near-identical examples produce a pattern that is too strict.
- Add negative examples for the mistakes you want to catch. A red row means the pattern accepts something it shouldn’t, or rejects a valid example.
- Turn on Allow any length when lengths vary freely, such as names or free-text IDs.
- For well-known formats (email, URL, UUID, dates, postcodes, semantic versions), start from a common pattern below instead.
The pattern uses JavaScript regex syntax, which works as-is in most languages. Check what each part does with the regex explainer, and test it on real text with the regex tester.
FAQ
How do I generate a regex from examples?
Type the strings that should match, one per line. A pattern is generated from their structure. Add strings that should not match to check the pattern against them.
Why is the generated regex too strict?
It only knows your examples. Add more varied examples, or turn on “Allow any length” so lengths are not fixed.
Can it generate a regex for email or phone numbers?
For well-known formats, use the common patterns list, which has tested patterns for email, URL, phone numbers, dates, IP addresses, UUIDs, postcodes and more.
Does the regex work in Python or Java?
Generated patterns use basic syntax (classes, quantifiers, anchors) that works the same in JavaScript, Python, Java, PHP, Go and .NET.
Is my data sent anywhere?
No. The pattern is generated and checked in your browser.