Regex is the single best AI use case in development: tedious to write, error-prone to read, and easy to test with examples. The trick is to brief the AI with the right kind of examples so its regex actually matches what you mean — and rejects what you don't.
Writing a regular expression by hand is rarely about cleverness — it's about not forgetting an edge case. AI is excellent at this exact kind of task, provided you steer it with examples instead of descriptions. This tutorial walks through the example-first prompt pattern and shows you how to test the resulting regex before it ever touches production data.
A regex has four parts you have to nail down: flavour (PCRE, JavaScript, Python re, POSIX), anchors (whole-string match vs find-in-text), character classes (ASCII vs Unicode), and flags (case sensitivity, dotall, multiline). Most regex bugs trace back to one of these being wrong by accident.
Think of regex prompts as test-driven specification: give the examples first, then describe the rule. The AI is then writing to a contract you can verify.
The instinctive prompt is "regex for email" or "regex for phone number". The AI returns a one-liner that is either too strict (rejects valid input) or too loose (accepts garbage).
Weak prompt
give me a regex to match indian phone numbers
What counts as a "valid Indian phone number" depends on whether you accept the +91 prefix, the 0 prefix, spaces and dashes, landlines vs mobiles. The AI guesses one shape — usually mobile, no spaces, no +91 — and you spend the next twenty minutes patching it.
Strong prompt
Write a regular expression for the JavaScript regex flavour (RegExp).
What it should match:
- Indian mobile numbers as they appear in user-submitted forms.
- Match the WHOLE input string (anchor both ends).
- Case-insensitive flag not needed.
SHOULD MATCH (positive examples):
9876543210
+919876543210
+91 98765 43210
+91-98765-43210
09876543210
MUST NOT MATCH (negative examples):
98765 (too short)
98765432101 (too long)
+9298765432 (wrong country code)
abc9876543210 (non-digit prefix)
+91 1234567890 (mobile must start with 6, 7, 8, or 9 after country code)
+91 (98765) 43210 (we don't accept parentheses)
Rules:
- Only digits (0–9), single optional '+91' or '91' prefix, optional spaces
or single hyphens as separators, mobile number is exactly 10 digits and
starts with 6, 7, 8, or 9.
- A leading '0' is allowed in place of '+91'.
Output:
1. The regex as a single JavaScript literal: /…/u
2. A 1-line description of what it does.
3. A table of all the example inputs above and whether your regex matches
each, so I can verify before using it.
The AI now writes against a precise spec. You get a regex, plus a verification table that immediately shows whether it gets every example right. If one is wrong, you reply with that example and the AI tightens the regex — a fast, controllable loop.
re, PCRE, POSIX, .NET. Each has different syntax for lookaheads, named groups, and Unicode.^…$) vs find-substring. Without this, the AI defaults to "find" and accepts inputs with garbage prefixes.Tip: When the AI's regex fails on a new edge case, don't ask it to "fix" the regex from scratch — paste the new failing example into the same chat and ask it to update the regex while keeping all previous examples correct. This iterative tightening is much more reliable than rewrites.
Pick a piece of input your project actually validates today (email, URL, postal code, ID format). Write the strong-prompt format with at least eight positive and eight negative examples. Compare the resulting regex to whatever your project currently uses.
Take an existing regex from your codebase and ask the AI:
Generate 10 inputs this regex should match and 10 it should reject. Then test the regex against each and show me the result.
Any surprises? Those are bugs hidden in plain sight.
Build a small regex prompt library — three to five reusable prompts for the inputs your team validates often. Paste examples from your actual data (anonymised). This becomes a high-leverage shared resource.
Sign in to join the discussion and post comments.
Sign inPrompt Engineering for Education & Learning
Use AI as your personal tutor. Learn how to study faster, create lesson plans, generate practice questions, master languages, and prepare for competitive exams with smart prompts.
Prompt Engineering for Business & Productivity
Use AI to work smarter — automate tasks, make better decisions, and communicate professionally. 12 practical business prompt tutorials for professionals.
Foundations of Prompt Engineering
The must-know basics of prompt engineering. Learn what prompts are, how AI models read them, and how to write clear instructions that get great results.
Prompt Engineering for Content & Copywriting
Write blogs, ads, emails, and social media content ten times faster with AI. 13 practical tutorials on prompt engineering for content creators and copywriters.
Prompt Engineering for Specific AI Tools
Tool-by-tool mastery — deep dives into ChatGPT, Claude, Gemini, GitHub Copilot, Midjourney, Stable Diffusion, and more. Learn the exact prompting techniques each platform rewards.
Prompt Engineering Projects & Real-World Applications
Twelve hands-on projects that turn prompt engineering theory into a portfolio. Build chatbots, content generators, RAG systems, and more.