关于 regex-visual-debugger
面向开发者的技能,用于分析、测试、解释和调试正则表达式,也可提出改进建议,并在 Python、JavaScript、Perl、Java、.NET 和 PHP 方言间转换。
主要功能
- 01逐部分解释正则结构
- 02测试字符串并展示匹配结果
- 03检查语法和方言问题
- 04生成正反向测试用例
- 05转换所列正则方言
适用场景
- 01调试无法匹配的表达式
- 02理解分组和量词
- 03用边界情况检查匹配
- 04在语言间转换表达式
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ onewave-ai/ claude-skills --skill regex-visual-debugger安装后在 Claude Code 中输入/regex-visual-debugger确认已加载。
详细介绍
正则表达式可视化调试器
用于交互式测试、解释和调试正则表达式的工具。
何时使用此技能
在用户执行以下操作时启用:
- 提供需要调试的正则表达式
- 询问“为什么我的正则表达式不起作用?”
- 需要解释正则表达式
- 想要针对字符串测试正则表达式
- 询问如何在不同风格的正则表达式之间转换(Python、JS 等)
- 需要正则表达式建议
- 提及正则表达式或模式匹配
说明
-
分析正则表达式
- 解析正则表达式结构
- 识别各个组成部分(分组、量词、字符类)
- 检查常见语法错误
- 验证指定风格的正则表达式语法
-
用通俗易懂的语言解释
- 逐部分解析模式
- 解释每个部分匹配的内容
- 描述模式的整体行为
- 说明量词的贪婪性
- 解释捕获组与非捕获组的区别
-
可视化分解
- 以层次结构展示模式
- 突出显示分组和选择分支
- 指出字符类和范围
- 标记锚点和边界
-
针对示例进行测试
- 测试提供的测试字符串
- 展示哪些字符串匹配,哪些不匹配
- 突出显示匹配部分
- 解释匹配成功或失败的原因
- 展示捕获组的内容
-
识别常见问题
- 特殊字符未转义
- 量词不正确
- 贪婪与非贪婪问题
- 锚点位置错误
- 分组未闭合
- 特定风格的不兼容问题
-
生成测试用例
- 创建应该匹配的字符串
- 创建不应该匹配的字符串
- 包含边界情况
- 测试边界条件
-
提出改进建议
- 更高效的模式
- 更易读的替代方案
- 性能优化
- 边界情况处理
-
在不同风格之间转换
- Python(re 模块)
- JavaScript
- Perl
- Java
- .NET
- PHP(PCRE)
输出格式
# Regex Analysis: `pattern`
## Plain English Explanation
This pattern matches [description]:
- `^` - Start of string
- `[A-Z]` - One uppercase letter
- `\d{3}` - Exactly 3 digits
- `$` - End of string
**Overall**: Matches strings like "A123", "Z999"
## Visual Structure
^ - 起始锚点 [A-Z] - 字符类(大写字母) \d{3} - 数字,恰好 3 次 $ - 结束锚点
## Test Results
### Matches
- `A123` -> Full match
- `Z999` -> Full match
### No Match
- `a123` -> Lowercase 'a' doesn't match [A-Z]
- `A12` -> Only 2 digits (needs 3)
- `A1234` -> Too many digits
## Capture Groups
1. Group 1: `[captured text]`
2. Group 2: `[captured text]`
## Issues Found
**Issue 1**: Pattern is too restrictive
- **Problem**: Doesn't handle lowercase letters
- **Fix**: Use `[A-Za-z]` instead of `[A-Z]`
## Suggested Improvements
```regex
# More flexible version
^[A-Za-z]\d{3,5}$
更改:
- 添加小写字母
- 将
{3}改为{3,5},匹配 3–5 位数字
生成的测试用例
应该匹配
A123
Z999
B456
不应该匹配
1ABC (starts with digit)
ABCD (no digits)
A12 (too few digits)
特定风格的说明
JavaScript:[任何 JavaScript 特定说明] Python:[任何 Python 特定说明]
转换为其他风格
JavaScript
const pattern = /^[A-Z]\d{3}$/;
const match = str.match(pattern);
Python
import re
pattern = r'^[A-Z]\d{3}$'
match = re.match(pattern, string)
性能说明
- 当前复杂度:O(n)
- 没有回溯问题
- 可考虑使用非捕获组
(?:...)来提升性能
## Examples
**User**: "Why doesn't this regex match emails: `\w+@\w+\.\w+`?"
**Response**: Analyze pattern → Explain it matches simple emails only → Show test cases (fails on "[email protected]") → Identify issues (doesn't handle special chars, multiple TLDs) → Provide improved pattern → Generate comprehensive test cases
**User**: "Explain this regex: `^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$`"
**Response**: Break down pattern (IP address matcher) → Explain each component → Show it matches valid IPs (0-255 per octet) → Provide test cases → Visualize structure
**User**: "Convert this Python regex to JavaScript: `(?P<name>\w+)`"
**Response**: Identify named capture group (Python feature) → Convert to JS equivalent → Explain differences → Show both versions with usage examples
## Best Practices
- Always test regex with edge cases
- Explain in plain English first
- Show concrete examples (not just theory)
- Highlight common pitfalls for each pattern
- Provide both positive and negative test cases
- Consider performance implications
- Note flavor-specific features
- Suggest simpler alternatives when possible
- Use non-capturing groups for performance
- Escape special characters properly
- Be explicit about case sensitivity
- Test with Unicode characters if relevant
评论
支持 Markdown