Loading...
Loading...
Generates, explains, and tests regular expression patterns. Builds patterns from positive and negative examples, breaks down each component with a readable explanation table, generates edge-case test suites, and provides usage examples in Python and JavaScript. Triggers on: "regex for", "regular expression", "pattern for", "match strings like", "extract from", "build regex", "create pattern", "explain this regex", "what does this regex do", "regex to match", "parse with regex", "validate format". Use this skill when building, explaining, or testing a regular expression pattern.
npx skill4agent add mathews-tom/praxis-skills regex-builder| File | Contents | Load When |
|---|---|---|
| Character class reference, Unicode categories, POSIX classes | Always |
| Quantifier behavior, greedy vs lazy vs possessive, backtracking | Pattern needs repetition |
| Validated patterns for email, URL, phone, IP, date, UUID, etc. | Common validation requested |
| Syntax differences between Python, JavaScript, PCRE, POSIX | Multi-language usage needed |
re| Component | Meaning |
|---|---|
| Start of string |
| One letter (upper or lower) |
| 3 to 5 digits |
| End of string |
## Regex Pattern: {Brief Description}
### Requirements
- **Must match:** {description of valid inputs}
- **Must reject:** {description of invalid inputs}
- **Flavor:** {Python re | JavaScript | PCRE}
### Pattern
```regex
{pattern}| Component | Meaning |
|---|---|
| {what it matches and why} |
| Group | Name | Captures | Example |
|---|---|---|---|
| 1 | {name} | {what} | {example value} |
| # | Input | Should Match | Reason |
|---|---|---|---|
| 1 | | Yes | {why — happy path} |
| 2 | | Yes | {why — boundary} |
| 3 | | No | {why — invalid} |
| 4 | | No | {why — near-miss} |
| 5 | `` (empty) | No | Empty input |
import re
pattern = re.compile(r'{pattern}')
# Match entire string
if pattern.fullmatch(text):
...
# Search within string
match = pattern.search(text)
if match:
captured = match.group(1)
# Find all matches
matches = pattern.findall(text)const pattern = /{pattern}/;
// Test
if (pattern.test(text)) { ... }
// Match
const match = text.match(pattern);
if (match) {
const captured = match[1];
}
// Find all
const matches = [...text.matchAll(/{pattern}/g)];
## Calibration Rules
1. **Correctness over cleverness.** A readable, slightly longer pattern is better than
a cryptic short one. `[A-Za-z0-9]` is clearer than `\w` when you specifically mean
alphanumeric without underscores.
2. **Test negatives as rigorously as positives.** A pattern that matches everything
technically matches all positive examples. Negative examples prevent over-matching.
3. **Anchor when appropriate.** `^\d{3}$` matches exactly 3 digits. `\d{3}` matches
3 digits anywhere in the string. State the anchoring intent explicitly.
4. **Avoid catastrophic backtracking.** Nested quantifiers like `(a+)+` cause exponential
time on non-matching input. Test with adversarial inputs.
5. **Named groups over numbered groups.** `(?P<year>\d{4})` (Python) or `(?<year>\d{4})`
(JS) is self-documenting. Use numbered groups only for simple patterns.
6. **Specify the flavor.** Python `re`, JavaScript, and PCRE have different feature sets.
Lookaheads, lookbehinds, and Unicode support vary.
## Error Handling
| Problem | Resolution |
|---------|------------|
| Insufficient examples | Ask for more. Minimum 3 positive, 3 negative. |
| Contradictory examples | Flag the contradiction. Ask which examples are correct. |
| Requirements too complex for regex | Suggest a parser instead. Regex cannot handle recursive structures (nested brackets, HTML). |
| Pattern causes backtracking | Rewrite with atomic groups or possessive quantifiers. Test with worst-case input. |
| Unicode requirements unclear | Ask if the pattern needs to handle non-ASCII. Default to ASCII unless specified. |
| Multiple valid patterns | Present the simplest one. Mention alternatives if they have meaningful tradeoffs (performance vs readability). |
## When NOT to Build Regex
Push back if:
- The input requires parsing a recursive grammar (HTML, JSON, nested expressions) — use a parser
- The validation is for a standard format with a library (email validation, URL parsing) — use the standard library
- The pattern is for security-critical input validation as the sole defense — regex is a first filter, not a security boundary
- The user wants to modify matched content in complex ways — regex replacement has limits; suggest code instead