agentleFS
ATOM00blueATOM00blue/agent-skills-pro/skills/regex-builder/SKILL.md

regex-builder

Skill0 starsChanged 6 months agoMITView on GitHub

Builds, explains, and tests regular expressions, and flags catastrophic-backtracking (ReDoS) risks. Translates plain-English requirements into a correct pattern with a token-by-token explanation and test cases. Use when the user needs a regex, asks what a pattern means, wants to validate/extract/replace text, or mentions regex, pattern matching, or ReDoS.

5 other projects use a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

3 rules. 0 are common, 2 are distinctive, and 1 is very specific to this project.

  • 0

    Common

    Most of the 3 edited versions we read keep these.

  • 2

    Distinctive

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Prefer RE2-style engines for untrusted input when the feature set allows.

    line 45
  2. Very specific:

    Default to anchoring (^...$) when validating a full value, and use the smallest character classes that satisfy the spec.

    line 49
  3. Distinctive:

    Email/URL "validation": no regex fully validates these.

    line 134

“Common” is measured: kept by at least 6 in 10 of the 3 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Regex Builder

Produce a correct, readable, safe regular expression — and prove it works.

Workflow

- [ ] 1. Restate the requirement: what must match, what must NOT match
- [ ] 2. Confirm the flavor (PCRE/Python/JS/Go/RE2) and flags
- [ ] 3. Build the pattern from anchored, named pieces
- [ ] 4. Explain it token by token
- [ ] 5. Give passing and failing test cases
- [ ] 6. Check for catastrophic backtracking (ReDoS)

Step 1 — Pin down the spec

Ambiguity is the main source of wrong regexes. Before writing anything, state explicitly:

  • Three to five strings that must match.
  • Three to five strings that must NOT match (the near-misses matter most).
  • Whether you're matching the whole string (anchor with ^...$) or finding substrings.

Step 2 — Know the flavor

Syntax and capabilities differ. Confirm which engine the regex runs in:

EngineNotes
JavaScript\d is Unicode-unaware by default; use u flag; lookbehind in modern engines only
Python reNamed groups (?P<name>...); re.VERBOSE allows whitespace/comments
PCRE / PHP / JavaRichest feature set; supports atomic groups and possessive quantifiers
Go / RE2 / RustLinear time, no backtracking — no backreferences or lookaround, but ReDoS-immune

Prefer RE2-style engines for untrusted input when the feature set allows.

Step 3 — Build from named, anchored pieces

Default to anchoring (^...$) when validating a full value, and use the smallest character classes that satisfy the spec. Build complex patterns from labeled sub-expressions rather than one dense line.

Reliable building blocks

\d              one digit (ASCII)            [A-Za-z]   one ASCII letter
\w              word char [A-Za-z0-9_]       \s         whitespace
.               any char except newline      [^...]     negated class
a?  a*  a+      0-1, 0+, 1+ (greedy)         a*?  a+?    lazy variants
{2,4}           between 2 and 4              (?:...)    non-capturing group
(?<name>...)    named capture group         \1 / \k<n> backreference
^   $   \b      start, end, word boundary    (?i)       case-insensitive (inline)
(?=...) (?!...) lookahead / negative lookahead

Step 4 — Always explain the pattern

Break the final regex into a token-by-token table so the user can verify and maintain it.

Step 5 — Provide test cases

List concrete strings that should match and should fail, including tricky near-misses.

Step 6 — ReDoS safety check (do this every time)

Catastrophic backtracking can hang a server on a short malicious input. Flag and fix any pattern where the input can be parsed many ways. The danger signs:

  • Nested quantifiers: (a+)+, (a*)*, (.*)*
  • Quantified group with overlapping alternation: (a|a)*, (\d+|\w+)*
  • Adjacent overlapping quantifiers: \d+\d+, .*.*
Dangerous:  ^(\w+\s?)*$        on "aaaaaaaaaaaaaaaaaaaa!"  -> exponential blowup
Safer:      ^\w+(?:\s\w+)*$    no overlap between the repeated pieces

Fixes: remove overlap so each character has one parse path; make a quantifier possessive (\w++) or wrap in an atomic group ((?>\w+)) in PCRE/Java; or switch to an RE2 engine. For "match up to a delimiter," prefer a negated class [^"]* over .*?.

Examples

Requirement: validate a US ZIP code (12345 or 12345-6789), full-string.

Pattern (JS/PCRE):

^\d{5}(?:-\d{4})?$

Explanation:

TokenMeaning
^start of string
\d{5}exactly five digits
(?:-\d{4})?optionally, a hyphen and four more digits (non-capturing)
$end of string

Tests: match 12345, 12345-6789; reject 1234, 123456, 12345-67, abcde.


Requirement: capture the host from an http(s) URL.

Pattern (Python, re.VERBOSE):

import re
url_re = re.compile(r"""
    ^https?://          # scheme
    (?P<host>[^/:\s]+)  # host: up to a slash, colon, or whitespace
    (?::\d+)?           # optional :port
    (?:/\S*)?$          # optional path
""", re.VERBOSE)
m = url_re.match("https://example.com:8080/path")
m.group("host")  # -> "example.com"

Note the host uses a negated class [^/:\s]+ rather than .+, which keeps it linear-time and stops at the right boundary.

Common edge cases

  • Email/URL "validation": no regex fully validates these; validate format loosely, then verify by sending a confirmation. Don't ship a 200-character email regex.
  • Unicode: \w and \d are ASCII-only in many engines; use \p{L} / \p{N} with the Unicode flag when you need international text.
  • Multiline: ^/$ match line boundaries only with the multiline flag; . excludes newlines unless the dotall/single-line flag is set.
  • Escaping in code: in most languages backslashes must be doubled in string literals, or use raw strings (r"..." in Python, backtick strings in Go).

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.