All tools
SkillCurated · reviewed

Regex Builder Skill

Updated Jul 7, 2026

A Claude Code skill that builds a regular expression from a plain-English spec, with annotated breakdown, a table of passing and failing test cases, and notes on edge cases and dialect differences (JavaScript, Python, PCRE).

What it does

  • /regex

    Build a regex from a plain-English description, with annotated breakdown and test cases.

  • /regex <spec>

    Build a regex for the given spec; optionally include example match and no-match strings.

Files (1)

SKILL.mdprimary · markdown · 3.6 KB
# Regex Builder Skill

---
slug: regex-builder-skill
version: 1.0.0
category: engineering
command: /regex
---

## What it does
Builds a regular expression from a plain-English description of the pattern.
Explains each part of the regex in an annotated breakdown, provides a table of
strings that should match and strings that should not, and calls out edge cases and
dialect differences (JavaScript/ECMAScript, Python re, PCRE, Go regexp).

## Trigger
Use this skill when asked to write, build, or explain a regular expression.
Typical invocations:
- "Write a regex that matches a US phone number"
- "Explain what this regex does: ^(?=.*[A-Z])(?=.*\d).{8,}$"
- `/regex` in Claude Code
- `/regex <spec>` where spec is the plain-English description of the pattern

## Input
Provide one or more of:
1. A plain-English description of what the regex must match (and optionally what it must not match)
2. Example strings that should match
3. Example strings that should not match
4. The regex dialect (JavaScript, Python, PCRE, Go — defaults to JavaScript/ECMAScript)
5. An existing regex you want explained or improved

## Method

1. **Identify the structure** — what is the overall shape? (email, URL, date, custom token, etc.)
2. **Choose anchors** — should the pattern match the whole string (`^`/`$`) or a substring?
3. **Build the pattern** — assemble the components; prefer named groups for readability.
4. **Annotate** — break the pattern into its parts and explain each in plain English.
5. **Test cases** — produce a table of strings with expected match/no-match and the reason.
6. **Flag dialect gaps** — note any features that behave differently across dialects (lookahead support, Unicode mode, possessive quantifiers).

### Rules
- Prefer readable patterns with named capture groups over dense one-liners.
- Do not use catastrophic backtracking patterns (nested quantifiers on the same character class).
- If the spec is ambiguous (e.g., "a phone number" could be many formats), state the assumption made and offer alternatives.

## Output format

1. The regex, ready to copy
2. **Annotated breakdown** — each component on its own line
3. **Test cases** table
4. **Edge cases and dialect notes**

## Example output

**Spec**: Match a semantic version string (e.g. 1.0.0, 2.13.4) — digits only, no pre-release suffix.

**Regex** (JavaScript)
```
/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$/
```

**Annotated breakdown**
```
^                    — anchor: start of string
(0|[1-9]\d*)        — major: 0, or a non-zero digit followed by any digits (no leading zeros)
\.                   — literal dot
(0|[1-9]\d*)        — minor: same rule
\.                   — literal dot
(0|[1-9]\d*)        — patch: same rule
$                    — anchor: end of string
```

**Test cases**

| String   | Matches | Reason |
|----------|---------|--------|
| 1.0.0    | yes     | valid semver |
| 2.13.4   | yes     | multi-digit minor |
| 0.0.1    | yes     | zeroes allowed |
| 01.0.0   | no      | leading zero in major |
| 1.0      | no      | missing patch segment |
| 1.0.0-rc | no      | pre-release suffix not in spec |

**Edge cases**: This pattern rejects leading zeros per the semver spec. If you need to accept
`01.02.03` (some legacy systems use it), replace each group with `\d+`.
Python's `re` module and PCRE behave identically here; Go's `regexp` package does not support
lookaheads but this pattern does not need them.

## Commands (Claude Code)

- `/regex` — Build a regex from a plain-English spec with breakdown and test cases.
- `/regex <spec>` — Build a regex for the given spec; optionally include example match/no-match strings.