跳到正文
Skillpedia
通用编程18k · #10706

regex-expert

它能做什么

这个技能指导编写和调试用于匹配文本的正则表达式,例如邮箱、编码或日志行。适合需要一个只找到正确文本、而不多找其他内容的模式的人。

适用场景

  • ✓当你需要一个能在一段文字中找到邮箱、日期或编码的模式时。
  • ✓当正则表达式匹配过多或过少,需要调试时。
  • ✓当你希望用通俗语言解释一个正则表达式,使其他人能够维护它时。

技能链接

https://github.com/RightNow-AI/openfang/blob/main/crates/openfang-skills/bundled/regex-expert/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/RightNow-AI/openfang/blob/main/crates/openfang-skills/bundled/regex-expert/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 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\b matches 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 quantifiers a++ (where supported).
  • Put the most likely alternative first in alternations: (?:com|org|net) if .com is most frequent.
  • Use \A and \z instead 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 (use s or DOTALL flag).
  • 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

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能