Regex
Never hand-wave a regex — test it against real samples before handing it over. A pattern
that "looks right" but fails on the user's actual input is worse than no pattern.
Test a pattern against samples
python3 - <<'PY'
import re
pat = r'\b\d{4}-\d{2}-\d{2}\b'
samples = ["due 2026-05-31 ok", "no date", "2026/05/31 wrong sep"]
for s in samples:
print(repr(s), "->", re.findall(pat, s))
PY
Extract with named groups
python3 - <<'PY'
import re
m = re.search(r'(?P<user>[\w.+-]+)@(?P<domain>[\w.-]+)', "ping alice.k+test@mail.example.com")
print(m.groupdict() if m else "no match")
PY
Find-and-replace with backreferences
python3 - <<'PY'
import re
print(re.sub(r'(\d{4})-(\d{2})-(\d{2})', r'\3/\2/\1', "2026-05-31")) # -> 31/05/2026
PY
Common building blocks
- Anchors:
^ start, $ end, \b word boundary.
- Classes:
\d digit, \w word char, \s space; negate with uppercase \D \W \S.
- Quantifiers:
* 0+, + 1+, ? 0/1, {m,n}; add ? for lazy (.*?).
- Groups:
(...) capture, (?:...) non-capture, (?P<name>...) named.
- Lookaround:
(?=...), (?!...), (?<=...), (?<!...).
Guidance
- Always show what it matches AND what it deliberately rejects, using the user's samples.
- Prefer explicit, readable patterns over clever unreadable ones; add
re.VERBOSE for
complex patterns and comment them.
- Warn about catastrophic backtracking (nested quantifiers like
(a+)+) on untrusted input.
- Note the regex flavor if it leaves Python (JS, PCRE, Go/RE2, grep) — escaping and
lookbehind support differ.
1---2name: regex3description: Write, explain, test, and debug regular expressions, and build them iteratively against real sample input. Use when users want a regex/pattern to match or extract text, ask why a pattern isn't matching, need to validate emails/URLs/dates/phone numbers, or want to do find-and-replace with capture groups. Triggers on mentions of regex, regular expression, pattern, match, extract, validate, find and replace, 正则, 正则表达式, 匹配, 提取, 替换.4license: Proprietary. LICENSE.txt has complete terms5---67# Regex89Never hand-wave a regex — test it against real samples before handing it over. A pattern10that "looks right" but fails on the user's actual input is worse than no pattern.1112## Test a pattern against samples1314```bash15python3 - <<'PY'16import re17pat = r'\b\d{4}-\d{2}-\d{2}\b'18samples = ["due 2026-05-31 ok", "no date", "2026/05/31 wrong sep"]19for s in samples:20 print(repr(s), "->", re.findall(pat, s))21PY22```2324## Extract with named groups2526```bash27python3 - <<'PY'28import re29m = re.search(r'(?P<user>[\w.+-]+)@(?P<domain>[\w.-]+)', "ping alice.k+test@mail.example.com")30print(m.groupdict() if m else "no match")31PY32```3334## Find-and-replace with backreferences3536```bash37python3 - <<'PY'38import re39print(re.sub(r'(\d{4})-(\d{2})-(\d{2})', r'\3/\2/\1', "2026-05-31")) # -> 31/05/202640PY41```4243## Common building blocks4445- Anchors: `^` start, `$` end, `\b` word boundary.46- Classes: `\d` digit, `\w` word char, `\s` space; negate with uppercase `\D \W \S`.47- Quantifiers: `*` 0+, `+` 1+, `?` 0/1, `{m,n}`; add `?` for lazy (`.*?`).48- Groups: `(...)` capture, `(?:...)` non-capture, `(?P<name>...)` named.49- Lookaround: `(?=...)`, `(?!...)`, `(?<=...)`, `(?<!...)`.5051## Guidance5253- Always show what it matches AND what it deliberately rejects, using the user's samples.54- Prefer explicit, readable patterns over clever unreadable ones; add `re.VERBOSE` for55 complex patterns and comment them.56- Warn about catastrophic backtracking (nested quantifiers like `(a+)+`) on untrusted input.57- Note the regex flavor if it leaves Python (JS, PCRE, Go/RE2, grep) — escaping and58 lookbehind support differ.