home / skills / jmerta / codex-skills / regex-builder

regex-builder skill

/regex-builder

This skill helps you build, test, and explain regular expressions across engines with runnable verification and concise rationale.

npx playbooks add skill jmerta/codex-skills --skill regex-builder

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
1.8 KB
---
name: regex-builder
description: Build, test, and explain regular expressions against sample text or files using CLI tools (rg, python) and specific regex flavors. Use when asked to craft, debug, or validate regexes or search patterns.
---
# Regex builder

## Goal
Produce a correct, testable regex with rationale and runnable verification.

## Inputs to confirm (ask if missing)
- Target regex flavor/engine (PCRE, Python, JavaScript, .NET, RE2, etc.).
- Example matches and non-matches (at least 3 each when possible).
- Scope: single-line vs multi-line; file types/paths.
- Output needs: capture groups, named groups, replacement template.

## Workflow
1) Gather samples
   - Ask for sample text or identify files.
   - If files exist, locate with `rg --files` and preview with `rg -n`.
2) Choose tool
   - Prefer `rg` for quick match checks; use `rg -P` for PCRE features.
   - Use Python for detailed group inspection when needed.
3) Build incrementally
   - Start with a minimal literal anchor; expand piece by piece.
   - Add anchors/boundaries; handle whitespace and separators explicitly.
4) Validate
   - Show a command to test against samples.
   - Confirm no false positives by testing non-matches.
5) Deliver
   - Provide final regex, flags, and explanation.
   - Include a test command and expected match summary.
   - Note any tradeoffs/backtracking risks.

## CLI snippets (use as needed)
- Ripgrep check:
  - `rg -n "<regex>" path\\to\\file`
  - `rg -n -P "<regex>" path\\to\\file`
- Python quick test (adjust quoting for the shell):
  - `python -c "import re,sys;pat=re.compile(r'<regex>');data=sys.stdin.read();print([m.group(0) for m in pat.finditer(data)])" < path\\to\\file`

## Deliverables
- Regex + flags and any capture group map.
- Test command(s) and summary of matches/non-matches.
- Short explanation of the approach and known limitations.

Overview

This skill builds, tests, and explains regular expressions tailored to a specific regex flavor and sample data. It produces a correct, runnable pattern, verification commands for common CLI tools (rg, python), and a brief rationale. The output includes flags, capture group mapping, and known tradeoffs to help you safely deploy the regex.

How this skill works

I first gather the target regex engine and a set of positive and negative examples. I then construct the pattern incrementally, using literal anchors, boundaries, and explicit whitespace handling. Finally I validate the regex with suggested ripgrep or Python commands and report matches, false positives, and limitations.

When to use it

  • Crafting a new regex for search, validation, or extraction across files
  • Debugging an existing pattern that produces unexpected matches or misses
  • Adapting a regex to a specific engine (PCRE, Python, JavaScript, RE2, .NET)
  • Verifying capture groups and producing a replacement template
  • Checking file-scoped searches with ripgrep or doing detailed group inspection with Python

Best practices

  • Provide at least three positive and three negative examples before building
  • Specify the regex flavor and whether multi-line or single-line scope is required
  • Start with a minimal literal match and expand with groups and anchors
  • Prefer non-greedy quantifiers and explicit character classes to reduce backtracking
  • Validate against real files using rg -P for PCRE or python for detailed captures

Example use cases

  • Extract ISO dates from log files while avoiding false matches like 9999-99-99
  • Build a JavaScript safe email validation with named capture groups for local/domain
  • Convert a loose grep search into a precise PCRE for ripgrep across a codebase
  • Fix a Python regex that drops group 2 on some lines by showing group mapping and test
  • Create a replacement template that swaps first/last name using captured groups

FAQ

What information do you need to start?

Tell me the regex flavor, a few matching and non-matching examples, scope (single/multi-line), and any required capture or replacement format.

Which tool do you use for quick checks?

I prefer ripgrep (rg) for fast file checks and use rg -P for PCRE. Use Python when you need detailed group output or cross-engine verification.