Haruto Tanaka
@haruto_tanaka • 1 months ago
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.
goalflavormust_matchmust_not_match{{goal}}{{flavor}}{{must_match}}{{must_not_match}}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
₹1234 (4+ digits, no commas) as not a match. Say if you want it accepted.Rs, INR). Add the i flag if you want rs 500.costs ₹499.) should still match.`js`(? | 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.₹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.