Skip to content

Build regex engine with capture-group replacement support #37

@dolph

Description

@dolph

Goal

Introduce an internal regex-based match/replace engine that supports capture groups ($1, $2, ...) in the replacement string, shared by both the content-rewrite and rename code paths.

This is the engine work that the user-facing -e/--regex flag will sit on top of. It can land without exposing the flag — existing literal mode continues to call the engine with the pattern escaped.

Scope

  • Use Go's standard regexp package (RE2 syntax — no catastrophic backtracking, predictable performance).
  • ReplaceAllString already supports $N and ${name} references; this is mostly plumbing.
  • Single shared engine API used by both rename and content paths so the syntax is identical.
  • Literal mode delegates to the same engine by escaping the pattern with regexp.QuoteMeta.

Suggested API shape

type Pattern struct {
    re   *regexp.Regexp
    repl string
}

func Compile(pattern, replacement string, literal bool) (*Pattern, error)
func (p *Pattern) Match(s string) bool
func (p *Pattern) Replace(s string) string

Both the file-content path and the rename path consume *Pattern — no separate logic per mode.

Acceptance

  • Internal engine compiles a pattern and applies replacement with captures
  • Literal mode round-trips through the same engine via regexp.QuoteMeta
  • No user-visible behavior change (no new flags exposed in this issue)
  • Benchmark: literal mode performance is within 5% of current implementation
  • Tests cover: literal-mode equivalence, capture groups, named groups, escape edge cases ($$, $0, out-of-range refs)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions