# Build and test a regex against your own match examples

> Describe what to match, give examples that should and shouldn't match, and get a regex with a token breakdown, a pass/fail table and extra edge cases.

- **Author:** [Haruto Tanaka (@haruto_tanaka)](https://promptabide.com/haruto_tanaka)
- **Tested on:** Claude · Opus 5.5
- **You fill in:** `goal`, `flavor`, `must_match`, `must_not_match`
- **Published:** 2026-08-03
- **Updated:** 2026-09-24
- **Tags:** `coding`, `testing`, `productivity`
- **Keywords:** regex generator prompt with test cases, write a regular expression with chatgpt, regex to match indian rupee amounts, regex for indian number format with commas, explain regex token by token
- **Views:** 1092
- **Likes:** 26

**Best for:** Anyone writing a regex for validation or extraction who wants proof it handles their real examples, not just a plausible pattern.

## Prompt

```
Write a regular expression for: {{goal}}
Regex flavor: {{flavor}}

Must match (and what to capture, if anything):
{{must_match}}

Must NOT match:
{{must_not_match}}

Rules:
1. Before writing it, list any ambiguity in my examples and the assumption you're making.
2. Give the pattern, then a table: token | what it does.
3. Test it against every example I gave, step by step, and show a pass/fail table. If anything fails, fix the pattern and test again before answering. Only show the final version.
4. Add 3 edge cases I didn't think of and say whether each matches.
5. Tell me if the pattern risks catastrophic backtracking, or if a parser would be a better tool than a regex for this job.
```

## Variables

- `{{goal}}` — What the regex should match or extract (e.g. Rupee amounts in free text, using Indian digit grouping)
- `{{flavor}}` — Regex engine or language (e.g. JavaScript (ES2018+))
- `{{must_match}}` — Strings that must match, one per line (e.g. ₹499, ₹1,23,456.50, Rs. 12,000)
- `{{must_not_match}}` — Strings that must not match (e.g. ₹1,23,4567, ₹12,34, $1,299)

## Example input

```
goal: Find rupee amounts in free text (product descriptions, chat messages) that use Indian digit grouping, and capture the number part.
flavor: JavaScript (ES2018+, lookbehind allowed)
must_match:
₹499
₹1,299
₹1,23,456.50
Rs. 12,000
INR 2,50,000
must_not_match:
₹1,23,4567
₹12,34
₹1,299.5
$1,299
```

## Output

*Produced by Claude · Opus 5.5.*

````
**Ambiguities and assumptions**
- Your examples always use grouping, so I'm treating `₹1234` (4+ digits, no commas) as **not** a match. Say if you want it accepted.
- Decimals: exactly two digits, or none.
- Prefixes are case-sensitive (`Rs`, `INR`). Add the `i` flag if you want `rs 500`.
- A trailing sentence full stop (`costs ₹499.`) should still match.

**Pattern**
```js
/(?<![A-Za-z])(?:₹|Rs\.?|INR)\s?(\d{1,2}(?:,\d{2})*,\d{3}|\d{1,3})(?:\.\d{2})?(?![\d,]|\.\d)/g
```

| Token | Meaning |
|---|---|
| `(?<![A-Za-z])` | not glued to a word (rejects `XINR`) |
| `(?:₹\|Rs\.?\|INR)` | currency prefix |
| `\s?` | optional space |
| `\d{1,2}(?:,\d{2})*,\d{3}` | Indian grouping: 1–2 digits, any number of `,dd`, then `,ddd` |
| `\|\d{1,3}` | or a plain amount under 1,000 |
| `(?:\.\d{2})?` | optional paise |
| `(?![\d,]\|\.\d)` | stop cleanly: no more digits, commas or decimal digits |

Group 1 is the number without paise.

**Test results**

| Input | Expected | Result |
|---|---|---|
| ₹499 | match | pass, group 1 = `499` |
| ₹1,299 | match | pass: `1,299` |
| ₹1,23,456.50 | match | pass: `1,23,456` |
| Rs. 12,000 | match | pass: `12,000` |
| INR 2,50,000 | match | pass: `2,50,000` |
| ₹1,23,4567 | no match | pass: `1,23,456` is followed by `7`, so the lookahead fails and no shorter split works |
| ₹12,34 | no match | pass: no `,ddd` group, and `12` is followed by a comma |
| ₹1,299.5 | no match | pass: `.5` isn't two digits, and `\.\d` in the lookahead blocks stopping at `1,299` |
| $1,299 | no match | pass: no prefix |

**Edge cases you didn't list**
- `₹1,234,567` (Western grouping): **no match**.
- `costs ₹499.` at the end of a sentence: **matches** `₹499`, because the full stop isn't followed by a digit.
- `₹0.50`: **matches** with group 1 = `0`. Filter zero amounts in code if they matter.

**Backtracking and alternatives**
There are no nested quantifiers over overlapping characters, so matching time stays linear. Regex is the right tool for *finding* amounts. To *use* them, strip commas and parse to integer paise in code. Never do arithmetic on the captured string.
````

## Why it works

The **must-not-match list** matters as much as the positives. It's what forced the trailing lookahead that rejects `₹1,23,4567` instead of quietly matching its first part, the most common bug in AI-written regexes. **"Test it step by step and fix before answering"** makes the model check its own pattern instead of shipping a first draft. **Stating assumptions up front** surfaces choices (4-digit amounts without commas, case) you would otherwise discover in production. The **backtracking warning** protects you from patterns that hang on long inputs.

## When not to use it

Don't rely on a regex alone for structured formats with checksums or grammar: emails you'll actually send to, URLs, HTML, JSON, tax IDs. Use a proper parser or validator library. The model's "test table" is reasoning, not execution, so paste the pattern into your test suite or a regex tester with the same examples before you trust it.

---

Canonical HTML: https://promptabide.com/bides/build-regex-with-test-cases
Agent guide: https://promptabide.com/llms.txt · https://promptabide.com/agent-instructions.md
Sitemap: https://promptabide.com/sitemap.xml
