replace

package
v0.5.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 9, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package replace provides runtime template parsing for regex replacement operations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Segment

type Segment struct {
	Type         SegmentType
	Literal      string // For SegmentLiteral: the literal text
	CaptureIndex int    // For SegmentCaptureIndex: 1-based index; for SegmentFullMatch: 0
	CaptureName  string // For SegmentCaptureName: the capture group name
}

Segment represents a parsed segment of a replacement template.

type SegmentType

type SegmentType int

SegmentType indicates the type of segment in a replacement template.

const (
	// SegmentLiteral represents literal text (no capture reference).
	SegmentLiteral SegmentType = iota
	// SegmentFullMatch represents a reference to the full match ($0).
	SegmentFullMatch
	// SegmentCaptureIndex represents a reference to a capture group by index ($1, $2, etc.).
	SegmentCaptureIndex
	// SegmentCaptureName represents a reference to a capture group by name ($name, ${name}).
	SegmentCaptureName
)

type Template

type Template struct {
	Original string
	Segments []Segment
}

Template represents a fully parsed replacement template.

func Parse

func Parse(template string) (*Template, error)

Parse parses a replacement template string into segments. Template syntax:

  • $0 or ${0}: full match
  • $1, $2, ..., $99 or ${1}, ${2}: capture group by index
  • $name or ${name}: capture group by name
  • $$: literal dollar sign
  • Everything else: literal text

func (*Template) ValidateAndResolve

func (t *Template) ValidateAndResolve(captureNames map[string]int, numCaptures int) ([]Segment, error)

ValidateAndResolve validates capture references against the pattern and resolves named captures to their indices. Returns a new slice of segments with all SegmentCaptureName converted to SegmentCaptureIndex.

Parameters:

  • captureNames: map of capture group names to their 1-based indices
  • numCaptures: total number of capture groups (not including full match)

Returns an error if any capture reference is invalid.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL