1---
2name: regex
3description: Write correct, efficient regular expressions across different engines.
4metadata:
5 source: https://skillmd.com/skills/modbender/regex
6 clawdbot:
7 emoji: 🔍
8 os:
9 - linux
10 - darwin
11 - win32
12---
13
14## Greedy vs Lazy
15
16- `.*` is greedy—matches as much as possible; `.*?` is lazy—matches minimum
17- Greedy often overshoots: `<.*>` on `<a>b</a>` matches entire string, not `<a>`
18- Default quantifiers `+ * {n,}` are greedy—add `?` for lazy: `+?` `*?` `{n,}?`
19
20## Escaping
21
22- Metacharacters need escape: `\. \* \+ \? \[ \] \( \) \{ \} \| \\ \^ \$`
23- Inside character class `[]`: only `]`, `\`, `^`, `-` need escape (and `^` only at start, `-` only mid)
24- Literal backslash: `\\` in regex, but in strings often need `\\\\` (double escape)
25
26## Anchors
27
28- `^` start, `$` end—but behavior changes with multiline flag
29- Multiline mode: `^` `$` match line starts/ends; without, only string start/end
30- `\A` always string start, `\Z` always string end (not all engines)
31- Word boundary `\b` matches position, not character—`\bword\b` for whole words
32
33## Character Classes
34
35- `[abc]` matches one of a, b, c; `[^abc]` matches anything except a, b, c
36- Ranges: `[a-z]` `[0-9]`—but `[a-Z]` is invalid (ASCII order matters)
37- Shorthand: `\d` digit, `\w` word char, `\s` whitespace; uppercase negates: `\D` `\W` `\S`
38- `.` matches any char except newline—use `[\s\S]` for truly any, or `s` flag if available
39
40## Groups
41
42- Capturing `()` vs non-capturing `(?:)`—use `(?:)` when you don't need backreference
43- Named groups: `(?<name>...)` or `(?P<name>...)` depending on engine
44- Backreferences: `\1` `\2` refer to captured groups in same pattern
45- Groups also establish scope for alternation: `cat|dog` vs `ca(t|d)og`
46
47## Lookahead & Lookbehind
48
49- Positive lookahead `(?=...)`: assert what follows, don't consume
50- Negative lookahead `(?!...)`: assert what doesn't follow
51- Positive lookbehind `(?<=...)`: assert what precedes
52- Negative lookbehind `(?<!...)`: assert what doesn't precede
53- Lookbehinds must be fixed-width in most engines—no `*` or `+` inside
54
55## Flags
56
57- `i` case-insensitive, `m` multiline (^$ match lines), `g` global (find all)
58- `s` (dotall): `.` matches newline—not supported everywhere
59- `u` unicode: enables `\p{}` properties, proper surrogate handling
60- Flags syntax varies: `/pattern/flags` (JS), `(?flags)` inline, or function arg (Python `re.I`)
61
62## Engine Differences
63
64- JavaScript: no lookbehind until ES2018; no `\A` `\Z`; no possessive quantifiers
65- Python `re`: uses `(?P<name>)` for named groups; no `\p{}` without `regex` module
66- PCRE (PHP, grep -P): full features; possessive `++` `*+`; recursive patterns
67- Go: RE2 engine, no backreferences, no lookahead—guaranteed linear time
68
69## Performance
70
71- Catastrophic backtracking: `(a+)+` against `aaaaaaaaaab` is exponential—avoid nested quantifiers
72- Possessive quantifiers `++` `*+` prevent backtracking—use when backtracking pointless
73- Atomic groups `(?>...)` don't give back chars—similar to possessive
74- Anchor patterns when possible—`^prefix` is O(1), unanchored `prefix` is O(n)
75
76## Common Mistakes
77
78- Email validation: RFC-compliant regex is 6000+ chars—use simple check or library
79- URL matching: edge cases are endless—use URL parser, regex for quick extraction only
80- Don't use regex for HTML/XML—use a parser; regex can't handle nesting
81- Forgetting to escape user input—regex injection is real; use literal escaping functions
82
83## Testing
84
85- Test edge cases: empty string, special chars, unicode, very long input
86- Visualize with tools: regex101.com shows matches and explains
87- Check which engine documentation you're reading—features vary significantly