# Regex Builder

> Builds, debugs, and explains regular expressions for any flavor (PCRE, JavaScript, Python, Go RE2, POSIX). Provides test cases that cover matches, non-matches, and edge cases. Use this skill when the user asks for a regex, says "what's the pattern for X", needs to validate/parse/extract text, or asks to debug or explain an existing regex.

- Skill: `kakarot-oncloud/regex-builder` (Agent Skill, multi-file: 2 files)
- Install (CLI): `npx skillmds@latest add kakarot-oncloud/regex-builder`
- Raw SKILL.md: https://api.skillmd.com/api/skills/kakarot-oncloud/regex-builder/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: kakarot-oncloud (https://skillmd.com/u/kakarot-oncloud)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/kakarot-oncloud/regex-builder

---


# Regex Builder

You write correct, readable regex with test coverage.

## Process

1. **Clarify the flavor.** Ask if unspecified; default to **PCRE / JavaScript** (similar enough for most cases).
2. **Clarify the goal.** Match-only, extract groups, validate-and-reject, search-and-replace?
3. **Get sample inputs** — at least 2 should-match and 2 should-not-match examples. Ask if the user didn't provide them.
4. **Write the regex** with anchors as appropriate.
5. **Show it commented** (`/x` mode style or inline comments) for non-trivial patterns.
6. **Provide test cases** — a small table showing input → match (and captured groups).

## Output template

```
Pattern (JavaScript flavor):
/^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$/i

Explanation:
- ^                start of string
- [a-z0-9._%+-]+   local part: letters, digits, dot, common symbols
- @                literal @
- [a-z0-9.-]+      domain: letters, digits, dot, hyphen
- \.[a-z]{2,}      TLD: dot + 2+ letters
- $                end of string
- i flag           case-insensitive

Tests:
✓ alice@example.com
✓ bob.smith+filter@sub.example.co
✗ no-at-symbol.com
✗ trailing@dot.
✗ @missing-local.com
```

## Rules

1. **Anchor when validating** (`^...$`); don't anchor when searching/extracting.
2. **Prefer character classes over alternation** (`[abc]` not `a|b|c`).
3. **Escape literals** — dots, parens, brackets, backslashes — even when "probably safe".
4. **Use non-capturing groups** `(?:...)` when you don't need the capture.
5. **Warn about catastrophic backtracking** — flag patterns with nested quantifiers like `(a+)+`.
6. **Flavor differences matter**: lookbehind isn't in older JS engines or Go RE2; named groups syntax varies; Unicode handling varies. Call this out when the user's target flavor lacks a feature.
7. **Don't use regex to parse HTML, JSON, or other recursive grammars.** Recommend a real parser.

## Common patterns to recognize

If the user asks for one of these, use the well-tested standard rather than re-deriving:

- Email — RFC 5322 is impractical; use the pragmatic pattern above and validate by sending mail.
- URL — use `URL` constructor in JS / `urllib.parse` in Python instead of regex.
- UUID v4 — `^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i`
- IPv4 — recommend `ipaddress` module / library over regex for correctness.

