Build a regular expression from examples
Prompt that builds a readable regular expression from matching and non-matching examples, explains it and checks it against every example. Paste it into an AI tool of your choice, which then builds the expression.
- Topic
- Coding & agents
- Type
- Prompt
- Level
- Intermediate
- Time
- about 5 min
Results depend on the tool, the model and what you enter.
Result preview
Example: written by our editors, not real AI output.
Pattern: three capital letters, a hyphen, exactly five digits. ^INV-\d{5}$ ^ · start of line INV- · fixed text \d{5} · exactly five digits $ · end of line INV-00421 · matches INV-31457 · matches INV-421 · does not match inv-00421 · does not match Edge cases: INV-004210 (six digits) does not match; a trailing space does not match.
Fill in
What you type stays in this browser window. We store and send nothing.
- A-1234 and B-9876
- INV-00421 and INV-31457
- jane.doe@example.com
- +44 20 7946 0958
- v2.14.0 and v10.0.3
- A-123 and a-1234
- INV-421 and inv-00421
- jane.doe@
- 020-7946-0958
- 2.14 and v2.14
- JavaScript
- Python
- PCRE or PHP
- Java
- search and replace in an editor
Your prompt
You have edited the prompt. Changes in the fields above no longer overwrite it.
This is an instruction for an AI, not the result itself.
Where this prompt works
These AI tools are a good fit for this prompt:
How to use it
- Fill in the fields or tap a suggestion. Leave out names, addresses and other personal data.
- Copy the finished prompt into an AI tool of your choice.
- Test the expression with your own data and add any failed cases as new examples.
Why does this work?
Counter-examples force the model to draw the boundary precisely. Checking every example individually makes mistakes visible at once.
Tips
- Regular expressions are poor at fully validating complex formats such as email addresses or date logic. For real validity, use a library or a second check in code.