Skip to contents

Handles static and dynamic replacements, including capture group references.

Usage

compute_replacement(
  text,
  pattern,
  match,
  replacement,
  call = rlang::caller_env()
)

Arguments

text

Text content as a single string.

pattern

Pattern to search for, matched using stringr (ICU regular expressions). Either:

match

Matched strings.

replacement

Replacement to associate with each match. Replacements are computed immediately during the search and stored in the result. Either:

  • NULL (default): no replacement. replace_files() cannot be called without setting replacements first.

  • A plain string, used literally as replacement text.

  • A string with backreferences of the form \1, \2, etc., replaced with the corresponding capture group from pattern.

  • A function, called once per file with a character vector of all matches found in that file, and expected to return a character vector of the same length (e.g. toupper).

  • A function wrapped with with_capture_groups_matrix(), called once per file with a character matrix where the first column is the full match and the remaining columns are the capture groups.

Value

A character vector of replacements, one per match.