regex-builder

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Regex builder

正则表达式构建器

Goal

目标

Produce a correct, testable regex with rationale and runnable verification.
生成准确、可测试的正则表达式,附带设计原理和可运行的验证逻辑。

Inputs to confirm (ask if missing)

需要确认的输入项(缺失时请询问)

  • Target regex flavor/engine (PCRE, Python, JavaScript, .NET, RE2, etc.).
  • Example matches and non-matches (at least 3 each when possible).
  • Scope: single-line vs multi-line; file types/paths.
  • Output needs: capture groups, named groups, replacement template.
  • 目标正则表达式流派/引擎(PCRE、Python、JavaScript、.NET、RE2等)。
  • 匹配示例和不匹配示例(尽可能各提供至少3个)。
  • 适用范围:单行vs多行;文件类型/路径。
  • 输出需求:捕获组、命名组、替换模板。

Workflow

工作流

  1. Gather samples
    • Ask for sample text or identify files.
    • If files exist, locate with
      rg --files
      and preview with
      rg -n
      .
  2. Choose tool
    • Prefer
      rg
      for quick match checks; use
      rg -P
      for PCRE features.
    • Use Python for detailed group inspection when needed.
  3. Build incrementally
    • Start with a minimal literal anchor; expand piece by piece.
    • Add anchors/boundaries; handle whitespace and separators explicitly.
  4. Validate
    • Show a command to test against samples.
    • Confirm no false positives by testing non-matches.
  5. Deliver
    • Provide final regex, flags, and explanation.
    • Include a test command and expected match summary.
    • Note any tradeoffs/backtracking risks.
  1. 收集样本
    • 索要示例文本或定位相关文件。
    • 如果存在文件,用
      rg --files
      定位,用
      rg -n
      预览内容。
  2. 选择工具
    • 优先使用
      rg
      进行快速匹配检查;需要PCRE特性时使用
      rg -P
    • 需要详细检查分组信息时使用Python。
  3. 增量构建
    • 从最小的字面量锚点开始,逐步扩展。
    • 添加锚点/边界;显式处理空格和分隔符。
  4. 验证
    • 给出针对样本测试的命令。
    • 通过测试不匹配样本来确认无误报。
  5. 交付
    • 提供最终正则表达式、标志位和说明。
    • 包含测试命令和预期匹配结果汇总。
    • 标注所有取舍/回溯风险。

CLI snippets (use as needed)

CLI代码片段(按需使用)

  • Ripgrep check:
    • rg -n "<regex>" path\\to\\file
    • rg -n -P "<regex>" path\\to\\file
  • Python quick test (adjust quoting for the shell):
    • python -c "import re,sys;pat=re.compile(r'<regex>');data=sys.stdin.read();print([m.group(0) for m in pat.finditer(data)])" < path\\to\\file
  • Ripgrep检查:
    • rg -n "<regex>" path\\to\\file
    • rg -n -P "<regex>" path\\to\\file
  • Python快速测试(根据shell调整引号):
    • python -c "import re,sys;pat=re.compile(r'<regex>');data=sys.stdin.read();print([m.group(0) for m in pat.finditer(data)])" < path\\to\\file

Deliverables

交付物

  • Regex + flags and any capture group map.
  • Test command(s) and summary of matches/non-matches.
  • Short explanation of the approach and known limitations.
  • 正则表达式 + 标志位和所有捕获组映射。
  • 测试命令和匹配/不匹配结果汇总。
  • 实现思路的简短说明和已知限制。