agentleFS
daizhouchendaizhouchen/openclaw-skills/skills/regex-wizard/SKILL.md

regex-wizard

Skill0 starsChanged 47 days agoMITView on GitHub

根据文本匹配需求编写或解释正则表达式,并按需生成 SVG 铁路图与交互测试页面。用于正则设计、排错与可视化。

1 other project uses a version of this.

Adopted by

Most recently taken up by

  • Other versions1 project uses a version of this.

    Word for word · 0

    None found.

    Edited · 0

    None found.

    Wrote their own version · 1

    1. ishandutta2007/Awesome-Claude-Skills★ 16same name

    How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.

  • In this repository9 other files it gives its agents.

The file

Files
SKILL.md

regex-wizard

Resolve <skill_dir> to the directory containing this SKILL.md. Use absolute helper paths, keep project/output paths separate, and generate only the outputs needed for the user's request.

根据自然语言描述生成正则表达式,附带铁路图可视化和交互式测试页面。

工作流程

Step 1: 理解意图

分析用户的自然语言描述,确定:

  • 需要匹配的文本模式类型
  • 是否需要捕获组(提取子串)
  • 目标编程语言/环境(Python、JavaScript、Go 等)
  • 边界条件和特殊要求(如:仅完整匹配、忽略大小写等)

先查阅下方 常用模式速查表,如果已有现成模式可直接复用或微调。

Step 2: 生成正则表达式

根据分析结果编写正则表达式,遵循以下原则:

  1. 正确性优先:先确保匹配所有正例,再排除反例
  2. 可读性:合理使用命名捕获组 (?P<name>...) 和注释模式 (?x)
  3. 性能:避免灾难性回溯,仅在目标引擎支持时使用占有量词或原子组
  4. 兼容性:标注所用语法的引擎兼容性(PCRE / ECMAScript / RE2 等)

输出格式:

正则表达式: <pattern>
引擎兼容:  <PCRE / ECMAScript / RE2 / ...>
标志:      <g/i/m/s/x 及其含义>

Step 3: 可视化 —— 铁路图(按需)

使用 scripts/railroad.py 生成 SVG 铁路图:

python "<skill_dir>/scripts/railroad.py" "<regex_pattern>" -o output.svg

颜色编码:

颜色含义
蓝色字面量 (literals)
绿色字符类 (character classes)
橙色量词 (quantifiers)
紫色分组 (groups)

将生成的 SVG 展示给用户,并逐段解释每个节点的含义。

Step 4: 交互式测试页面(按需)

使用 assets/tester_template.html 模板生成测试页面:

  1. 将正则表达式预填入输入框
  2. 生成 5 个正例和 5 个反例作为测试用例
  3. 输出完整 HTML 文件供用户在浏览器中打开
# 模板已内置实时匹配高亮、捕获组详情、标志切换等功能
# 只需将正则和测试用例填入模板即可

Step 5: 代码片段

为用户目标语言提供代码片段;以下 Python 和 JavaScript 示例按需选用:

Python:

import re

pattern = re.compile(r"<regex>", re.IGNORECASE)
matches = pattern.findall(text)
for m in matches:
    print(m)

JavaScript:

const pattern = /<regex>/gi;
const matches = text.matchAll(pattern);
for (const match of matches) {
  console.log(match[0], match.groups);
}

如用户指定其他语言(Go、Java、Rust 等),额外生成对应片段。


常用模式速查表

模式正则表达式说明
邮箱地址[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}通用邮箱匹配
URLhttps?://[a-zA-Z0-9.-]+(?:\.[a-zA-Z]{2,})(?:/[^\s]*)?HTTP/HTTPS 链接
中国手机号1[3-9]\d{9}中国大陆 11 位手机号
固定电话(?:0\d{2,3}-?)?\d{7,8}区号可选的固话
IPv4 地址(?:25[0-5]|2[0-4]\d|[01]?\d\d?)(?:\.(?:25[0-5]|2[0-4]\d|[01]?\d\d?)){3}0.0.0.0 ~ 255.255.255.255
日期 (YYYY-MM-DD)\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])ISO 8601 日期
日期 (DD/MM/YYYY)(?:0[1-9]|[12]\d|3[01])/(?:0[1-9]|1[0-2])/\d{4}欧式日期
时间 (HH:MM:SS)(?:[01]\d|2[0-3]):[0-5]\d:[0-5]\d24 小时制
中文字符[\u4e00-\u9fa5]+连续中文
身份证号[1-9]\d{5}(?:19|20)\d{2}(?:0[1-9]|1[0-2])(?:0[1-9]|[12]\d|3[01])\d{3}[\dXx]18 位二代身份证
邮政编码[1-9]\d{5}中国六位邮编
十六进制颜色#(?:[0-9a-fA-F]{3}){1,2}#RGB 或 #RRGGBB
HTML 标签<([a-zA-Z][a-zA-Z0-9]*)\b[^>]*>.*?</\1>简单 HTML 标签对
用户名[a-zA-Z][a-zA-Z0-9_-]{2,15}3-16 位,字母开头
强密码(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*]).{8,}至少 8 位,含大小写、数字、特殊字符

注意事项

  • 正则表达式引擎之间存在差异,务必标注兼容性
  • 避免使用灾难性回溯模式(如 (a+)+)
  • 对于复杂匹配需求,建议分步处理而非写一个超长正则
  • 中文相关正则注意 Unicode 范围的完整性
  • 生成的测试页面仅使用 JavaScript 引擎(ECMAScript),与其他引擎可能存在差异

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.