You are viewing v1 of 1. Go to the current version · All versions

Build and test a regular expression

Describe what you want to match and get a commented regex with matching and non-matching test cases.

Change note: Initial version

Prompt

Build a regular expression for this task.

What it should match: {{goal}}
Regex flavor: {{flavor}}
Examples that MUST match:
{{should_match}}

Examples that must NOT match:
{{should_not_match}}

Deliver:
1. The regex on its own line, in a code block, ready to paste.
2. A token-by-token breakdown in a table (part, meaning).
3. A test table covering every example I gave plus at least 6 more you invent, including tricky edge cases. For each row show the input, expected result and why. Walk through each case carefully so the table is correct for this flavor.
4. Known limits: what it intentionally does not handle, catastrophic backtracking risks, and anything better done with a parser or a validation library instead.
5. A usage snippet showing how to apply it with captured groups in {{language}}.

Use only features supported by the stated flavor. Prefer the simplest pattern that satisfies all examples over a clever one.

Variables

  • {{goal}}What to matchtext

    Default: US-style phone numbers with optional country code, capturing area code, prefix and line number

  • {{flavor}}Regex flavorselect

    Options: PCRE, JavaScript, Python re, Java, Go RE2, .NET, POSIX ERE

    Default: PCRE

  • {{should_match}}Should matchlong

    Default: (415) 555-0132 415-555-0132 +1 415 555 0132 4155550132

  • {{should_not_match}}Should not matchlong

    Default: 555-0132 (415) 5550-132 +44 415 555 0132

  • {{language}}Language for usage snippettext

    Default: Python

Example output

**Regex**
```
^(?:\+1[\s.-]?)?\(?([2-9]\d{2})\)?[\s.-]?(\d{3})[\s.-]?(\d{4})$
```

| Part | Meaning |
|---|---|
| `(?:\+1[\s.-]?)?` | optional +1 prefix with optional separator |
| `\(?([2-9]\d{2})\)?` | area code, optionally in parentheses, cannot start with 0 or 1 |
| `[\s.-]?` | optional space, dot or dash |
| `(\d{3})` / `(\d{4})` | prefix and line number |

**Tests**
| Input | Match | Why |
|---|---|---|
| (415) 555-0132 | yes | parenthesized area code |
| +1 415 555 0132 | yes | prefix with spaces |
| 555-0132 | no | no area code |
| +44 415 555 0132 | no | only +1 is allowed |
| 115-555-0132 | no | area code starts with 1 |
| (415 555-0132 | yes* | unbalanced parenthesis is accepted; see limits |

**Limits:** it does not enforce balanced parentheses; for real validation use a phone library. No nested quantifiers, so backtracking is bounded.

```python
m = re.match(PATTERN, text)
area, prefix, line = m.groups() if m else (None,)*3
```

Notes

Requiring a test table, including negative cases, forces the model to check its own pattern. The flavor selector matters because lookbehind and Unicode support differ between engines.

Build and test a regular expression (v1) | LazyPrompt