regex-pro
Write, test, and debug regular expressions with pattern recipes, engine quirks, and performance safety.
- License 许可
- MIT
- 最近更新
- 2026-11-07
- Stars
- ★ 0
- Forks
- 1
01 前言 Front Matter
- name
- regex-pro
- category
- utilities
- description
- Write, test, and debug regular expressions with pattern recipes, engine quirks, and performance safety.
03 SKILL.md 正文
Overview
Regular expressions are a precision instrument most people wield like a hammer. This skill covers writing correct, readable, safe regex: the core syntax, pattern recipes for common tasks, testing methodology, engine differences, and catastrophic backtracking — the performance trap that turns a bad pattern into a denial of service.
When to use
-
Writing patterns for validation, extraction, or search-and-replace
-
Debugging a regex that doesn't match (or matches too much)
-
Choosing between regex and simpler string methods
-
Reviewing regex for performance and safety
-
Learning regex systematically rather than by Stack Overflow
Core concepts
-
- The essential syntax. Literals,
.(any),\d \w \s(digit/word/space) and negations\D \W \S, quantifiers* + ? {n,m}, anchors^ $ \b, groups()with alternation|, character classes[]with ranges and negation[^]. This covers 90% of real patterns.
- The essential syntax. Literals,
-
- Greedy vs lazy.
.*grabs as much as possible (greedy);.*?grabs as little as possible (lazy).<.*>on<b>hi</b>matches the whole string;<.*?>matches<b>. Default greed causes most "it matched too much" bugs.
- Greedy vs lazy.
-
- Groups and backreferences.
(...)captures;(?:...)groups without capturing (prefer for performance and clarity);\1refers back to group 1. Named groups(?P<name>...)(Python) make complex patterns readable.
- Groups and backreferences.
-
- Anchors change everything.
^\d+$validates the entire string is digits;\d+without anchors finds digits anywhere. Validation without anchors isn't validation — it's searching.
- Anchors change everything.
-
- Engine differences. PCRE, JavaScript, Python
re, Go, Java — lookbehind support, named group syntax, and Unicode handling vary. Test in the target engine, not just a generic tester.
- Engine differences. PCRE, JavaScript, Python
-
- Catastrophic backtracking. Nested quantifiers like
(a+)+$on non-matching input cause exponential blowup — a 30-character string can hang for hours. This is a security issue in servers. Recognize the shape: quantified group containing quantifiers, especially with overlapping character classes.
- Catastrophic backtracking. Nested quantifiers like
Practical workflow
-
- Define the language precisely. What exactly should match? Write 5 positive and 5 negative examples before writing the pattern. Vague requirements produce vague patterns.
-
- Build incrementally. Start with the simplest core, test, then add one element at a time.
^\d{4}→^\d{4}-\d{2}→^\d{4}-\d{2}-\d{2}$. Each step verified before the next.
- Build incrementally. Start with the simplest core, test, then add one element at a time.
-
- Test both sides. Every pattern gets positive tests (must match) and negative tests (must not match) — including adversarial inputs: empty strings, unicode, newlines, very long strings.
-
- Prefer clarity. Named groups, comments (
(?x)verbose mode in Python), and breaking complex patterns into multiple simpler checks. A regex nobody can read is a bug waiting to happen.
- Prefer clarity. Named groups, comments (
-
- Consider alternatives. Email validation via regex is a famous tarpit — use a parser/library. HTML parsing with regex is a classic mistake — use a parser. Regex excels at: delimited formats, log lines, simple tokens, search/replace.
-
- Performance-check risky patterns. Test with pathological inputs (long non-matching strings). If it hangs, rewrite: possessive quantifiers where supported, atomic groups, or restructure to avoid nested quantifiers.
Common recipes:
-
Email (pragmatic, not RFC-perfect):
^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$ -
URL-ish:
https?://[^\s/$.?#].[^\s]* -
ISO date:
^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$ -
Whitespace trim:
^\s+|\s+$(replace with empty) -
Quoted string (simple):
"([^"\\]|\\.)*"(handles escapes, avoids catastrophic backtracking)
Common pitfalls
-
- Unanchored validation.
/\d+/"validates"abc123as numeric. Anchor both ends for validation.
- Unanchored validation.
-
- Greedy dot.
".*"matching across multiple quoted strings. Use lazy.*?or negated classes[^"]*.
- Greedy dot.
-
- Forgetting to escape.
.([+*?in literals. In most languages, prefer raw strings (r"\d+\.\d+") to avoid backslash-escaping hell.
- Forgetting to escape.
-
- Catastrophic backtracking.
(x+x+)+ystyle patterns on user input. Audit any regex that processes untrusted input — it's a ReDoS vector.
- Catastrophic backtracking.
-
- Over-engineering. A 200-character email regex that's still wrong. Match the requirement's actual strictness; use libraries for genuinely complex grammars.
-
- Not testing the engine. A pattern tested on regex101 with PCRE flavor failing in JavaScript (no lookbehind in older JS) or Go (no backreferences). Always verify in the deployment engine.
04 同仓库更多 Skills
web-scrape
Turn web pages into LLM-ready markdown via the Firecrawl API (renders JS, anti-bot handling). Use when browser_open/fetch fails on JS-heavy pages, or when you need clean markdown/structured extraction from a site. Requires FIRECRAWL_API_KEY.
design-taste-frontend
tasteskill v2, the anti-slop frontend skill. Art direction for websites, distilled from a study of 247 curated gallery sites (One Page Love, Awwwards Sites of the Day). Deals researched directions (idea, composition, type, palette, photographs, section blueprints) from a library, enforces a strict no-slop house style with a real-browser inspector, and runs a look-and-fix loop on screenshots. Use this whenever you build, design, redesign, restyle, polish or review a landing page, marketing site, portfolio, product page, one-pager, hero or any public-facing web page, in any stack (HTML/CSS, React, Next.js, Vue, Svelte, Astro, Tailwind) — also when the user says a page looks generic, templated, "AI-made", sloppy, or asks to make it look designed, premium, award-level or less like every other site. Not for dashboards or app screens.
test-driven-development
Use when implementing any feature or bugfix, before writing implementation code
systematic-debugging
Use when encountering any bug, test failure, or unexpected behavior, before proposing fixes
requesting-code-review
Use when completing tasks, implementing major features, or before merging to verify work meets requirements
brainstorming
You MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation).