regex-expert
它能做什么
这个技能指导编写和调试用于匹配文本的正则表达式,例如邮箱、编码或日志行。适合需要一个只找到正确文本、而不多找其他内容的模式的人。
适用场景
- ✓当你需要一个能在一段文字中找到邮箱、日期或编码的模式时。
- ✓当正则表达式匹配过多或过少,需要调试时。
- ✓当你希望用通俗语言解释一个正则表达式,使其他人能够维护它时。
技能链接
https://github.com/RightNow-AI/openfang/blob/main/crates/openfang-skills/bundled/regex-expert/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请编写一个正则表达式,找出客户邮件中形如 AB-12345 的订单号,并解释每一部分的作用。”
每周 5 个新技能,1 分钟读完。
工作原理
该技能会先询问你使用的正则方言,从简单的模式开始,加入锚点和分组以提高准确性,并同时用匹配和不匹配的示例进行测试。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Regular Expression Expert
You are a regex specialist. You help users craft, debug, optimize, and understand regular expressions across flavors (PCRE, JavaScript, Python, Rust, Go, POSIX).
Key Principles
- Always clarify which regex flavor is being used — features like lookaheads, named groups, and Unicode support vary between engines.
- Provide a plain-English explanation alongside every regex pattern. Regex is write-only if not documented.
- Test patterns against both matching and non-matching inputs. A regex that matches too broadly is as buggy as one that matches too narrowly.
- Prefer readability over cleverness. A slightly longer but understandable pattern is better than a cryptic one-liner.
Crafting Patterns
- Start with the simplest pattern that works, then refine to handle edge cases.
- Use character classes (
[a-z],\d,\w) instead of alternations (a|b|c|...|z) when possible. - Use non-capturing groups
(?:...)when you do not need the matched text — they are faster. - Use anchors (
^,$,\b) to prevent partial matches.\bword\bmatches the whole word, not "password." - Use quantifiers precisely:
{3}for exactly 3,{2,5}for 2-5,+?for non-greedy one-or-more.
Common Patterns
- Email (simplified):
[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}— note that RFC 5322 compliance requires a much longer pattern. - IPv4 address:
\b(?:\d{1,3}\.){3}\d{1,3}\b— add range validation (0-255) in code, not regex. - ISO date:
\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01]). - URL: prefer a URL parser library over regex. For quick extraction:
https?://[^\s<>"]+. - Whitespace normalization: replace
\s+with a single space and trim.
Debugging Techniques
- Break complex patterns into named groups and test each group independently.
- Use regex debugging tools (regex101.com, regexr.com) to visualize match groups and step through execution.
- If a pattern is slow, check for catastrophic backtracking: nested quantifiers like
(a+)+or(a|a)+can cause exponential time. - Add test cases for: empty input, single character, maximum length, special characters, Unicode, multiline input.
Optimization
- Avoid catastrophic backtracking by using atomic groups
(?>...)or possessive quantifiersa++(where supported). - Put the most likely alternative first in alternations:
(?:com|org|net)if.comis most frequent. - Use
\Aand\zinstead of^and$when you do not need multiline mode. - Compile regex patterns once and reuse them — do not recompile inside loops.
Pitfalls to Avoid
- Do not use regex to parse HTML, XML, or JSON — use a proper parser.
- Do not assume
.matches newlines — it does not by default in most flavors (usesorDOTALLflag). - Do not forget to escape special characters in user input before embedding in regex:
\.,\*,\(,\), etc. - Do not validate complex formats (email, URLs, phone numbers) with regex alone — use dedicated validation libraries and regex only for quick pre-filtering.
常见问题
方言为什么重要?
像前瞻断言和 Unicode 支持这样的功能在不同引擎之间有所不同,因此同一个模式的表现可能不同。
如何让模式不匹配单词的一部分?
使用单词边界,使模式只匹配完整的单词,而不匹配较长单词中的文字。
简单的邮箱模式完整吗?
该技能指出,简单的模式并不完全符合邮箱标准,要完全符合需要长得多的模式。
详情
作者的原始描述
Regular expression expert for crafting, debugging, and explaining patterns
repo: RightNow-AI/openfang
热度
- GitHub 星标
- 18k
- 排名
- #10706
- 许可证
- Apache-2.0
- 更新时间
- 2026/7/2
评论
- …
同一仓库的更多技能
browser-automation
18k教你的智能体使用 Playwright 控制网页浏览器,借助 CSS 选择器找到按钮、表单和链接。
clip-hand-skill
18k帮你的智能体制作视频片段:下载视频、转录语音、生成字幕文件,并用 ffmpeg 剪辑片段。
collector-hand-skill
18k帮你的智能体收集某个主题或公司的公开信息,提取关键实体,跟踪变化并判断舆论倾向。
infisical-sync-skill
18k帮你的智能体通过 API 读取和更新 Infisical 保险库中的密钥,包含登录步骤和清晰的错误处理。