before_match: before match

before_matchR Documentation

before match

Description

Augment pattern so that it can be used to match an entire string. Useful for complex find and replace operations, when used with capture_all_str.

Usage

before_match(...)

Arguments

...

pattern as in capture_first_vec.

Value

Pattern with two new groups, before (everything before ...) and match (text matching ... or blank for end of string).

Author(s)

Toby Hocking <toby.hocking@r-project.org> [aut, cre]

Examples


markdown_link <- list(
  "\\[",
  title=".*?",
  "\\]\\(",
  url="http.*?",
  "\\)")
markdown_subject <- "before [foo](http) between [bar text](http) after\n"
nc::capture_all_str(markdown_subject, markdown_link)

before_link <- nc::before_match(markdown_link)
(all_dt <- nc::capture_all_str(markdown_subject, before_link))

## before + match = full subject.
identical(all_dt[, paste(paste0(before, match), collapse="")], markdown_subject)

## replace with org link.
all_dt[, paste(paste0(before, ifelse(
  match=="", "", sprintf("[[%s][%s]]", url, title)
)), collapse="")]

## also works with no extra text before/after match.
nc::capture_all_str("[foo](http) between [bar text](http)", before_link)


nc documentation built on April 21, 2026, 9:06 a.m.