Web InnoventixPrompts

Build a regex from a plain-English description (with test cases)

Free

A fill-in-the-blank ChatGPT prompt that turns a plain-English rule into a working regex for your exact language — with a line-by-line breakdown and a pass/fail table of positive and negative test cases.

A single correct regex for your target engine, explained part by part and proven against edge-case tests, instead of a fragile pattern you can't trust.

This prompt

You are a regex engineer who writes precise, well-tested regular expressions and knows the syntax differences between engines (JavaScript, Python, PCRE, Java, .NET, Go RE2, Ruby, POSIX).

Your task: translate the plain-English description below into a single regular expression for {{regex_flavor}} that matches every case it should and rejects every case it shouldn't — then prove it with a test table.

What to match (plain English):
"""
{{match_description}}
"""

Target engine / language: {{regex_flavor}}

Sample text to test against (may be empty):
"""
{{sample_text}}
"""

Work through it in this order:
1. Restate the matching goal in one sentence, then list the concrete rules you are inferring — what must match, what must NOT match, and the edge cases (empty string, wrong length, extra characters, casing, leading/trailing whitespace). If the description is ambiguous, state the assumption you are using and continue.
2. Decide whether regex is the right tool. If the goal needs balanced or nested structures, arbitrary depth, or true spec-level validation (for example a fully RFC-compliant email), say so plainly and offer the closest safe approximation with a note on where it breaks.
3. Write the pattern using {{regex_flavor}} syntax specifically — respect that engine's support (or lack of support) for lookbehind, named groups, Unicode property escapes, and inline flags.
4. Anchor and bound the pattern deliberately: use ^ and $ for whole-string validation, or \b word boundaries for finding matches inside larger text. Choose greedy vs lazy quantifiers on purpose, and avoid nested quantifiers that can cause catastrophic backtracking.
5. Create test cases: at least 5 strings that SHOULD match and at least 5 that should NOT, including near-misses that are one character away from valid. If sample text was provided, pull examples from it.
6. Run the regex against every test case in your head, record the actual result next to the expected one, and if any case disagrees, fix the pattern and re-run before answering.

Return your answer in this format:
- Regex: the raw pattern on its own line in a code block (written for the engine, not for a string literal).
- Flags: which modifiers to enable (i, m, s, u, g, x) and one reason each is needed.
- Usage: a short {{regex_flavor}} snippet showing the pattern applied, with backslashes and quotes escaped correctly for that language's string rules.
- How it works: a bullet breakdown of each part of the pattern in plain English.
- Tests: a table with columns Input | Expected | Actual, listing the positive and negative cases from step 5.
- Notes: assumptions, limitations, and any "regex is not ideal here" caveats.

Constraints:
- Return one primary regex. If two reasonable interpretations exist, pick the most likely and note the alternative in a single line.
- Make the escaping in the raw pattern valid for {{regex_flavor}}, and make the escaping in the usage snippet match that language's string syntax.
- Use only syntax the chosen engine actually supports — for example, no lookbehind in Go RE2, and no variable-length lookbehind in JavaScript.
- Prefer explicit character classes over broad catch-alls, and keep the pattern only as complex as the rules require.
- If the rules cannot be met reliably with a regular expression, say so instead of shipping a pattern that looks correct but fails on edge cases.

7 fields still empty — the {{like_this}} slots fill in as you type.

See it in action

What this prompt produces with example details filled in:

**Goal:** Match a complete hex color code — a `#` followed by exactly 3 or exactly 6 hexadecimal digits — and reject anything else.

**Is regex the right tool?** Yes. This is a fixed, finite pattern with no nesting or arbitrary depth.

**Rules inferred:**
- Must start with `#`
- Then either exactly 3 or exactly 6 characters drawn from 0-9, a-f, A-F
- No other length (2, 4, 5, 7 digits all fail)
- Whole string is the color (validation), so anchor both ends and reject trailing whitespace

**Regex:**
```
^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$
```

**Flags:** none required — the character class already spans both cases. (Equivalent alternative: `^#([0-9a-f]{3}|[0-9a-f]{6})$` with the `i` flag.)

**Usage (JavaScript):**
```js
const isHexColor = (s) => /^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/.test(s);
```

**How it works:**
- `^` — start of string
- `#` — literal hash
- `(...|...)` — one of two branches
- `[0-9a-fA-F]{3}` — exactly three hex digits (shorthand form, e.g. `#FFF`)
- `[0-9a-fA-F]{6}` — or exactly six (full form, e.g. `#1a2b3c`)
- `$` — end of string, so `#FFF0` cannot sneak through

**Tests:**

| Input | Expected | Actual |
|-------|----------|--------|
| `#FFF` | match | match |
| `#1a2b3c` | match | match |
| `#ABCDEF` | match | match |
| `#000` | match | match |
| `#a1B2c3` | match | match |
| `FFF` (no #) | no match | no match |
| `#FF` (2 digits) | no match | no match |
| `#FFFF` (4 digits) | no match | no match |
| `#GGG` (invalid char) | no match | no match |
| `#1a2b3c ` (trailing space) | no match | no match |

**Notes:** The anchors make this a *validator* — the whole string must be a color. To *find* colors inside a larger block of text instead, drop `^` and `$`, add a word boundary, and list the 6-digit branch first so `#1a2b3c` is not partially matched as `#1a2`: `/#[0-9a-fA-F]{6}\b|#[0-9a-fA-F]{3}\b/g`.

Tips

Best for: Validating input formats like hex colors, phone numbers, dates, or slugs, Writing extraction patterns for logs, CSVs, or scraped text, Turning a written spec into a regex you can actually test, Fixing a regex that matches too much or too little
regexregular-expressionspattern-matchinginput-validationdeveloper-toolsjavascriptpythontesting

Built by Web Innoventix

Want the work done, not just prompted? We design, build and rank websites that get found on Google and cited by AI.

Get a free quote

More prompts

Browse all →
Development

Build a React component from a description

Turn a plain-English description into a production-ready, accessible React + Tailwind component with a usage example.

2 fieldsreacttailwind
Development

Senior code review that catches real bugs

Paste a snippet or PR diff and get a senior-engineer review: real issues ranked by severity, each with the exact input that breaks it and a concrete fix — not a wall of style nitpicks.

3 fieldscode-reviewpull-request
Development

Debug an error by root cause, not guesswork

Paste an error and its context to get a traced root-cause diagnosis, a fix that addresses the real cause, and a concrete way to verify the bug is gone.

2 fieldsdebuggingroot-cause-analysis
Development

Write thorough unit tests for a function or component

A fill-in prompt that turns any function or component into a runnable unit test suite covering happy paths, edge cases, boundaries, and error handling in your chosen framework.

4 fieldsunit testingunit tests
Development

Refactor a messy function into clean, maintainable code

A copy-paste ChatGPT prompt that refactors a messy function into clean, maintainable code without changing its behavior — it audits the code, rewrites it, explains each change, and suggests tests to prove nothing broke.

4 fieldsrefactoringclean-code
Development

Write a clear, conventional git commit message from a diff

A fill-in prompt that turns any git diff into a clean, convention-compliant commit message with a tight subject line and a what-and-why body a reviewer can trust.

3 fieldsgitcommit-message
Chat with us