Learn R Programming

nc (version 2026.2.20)

before_match: 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.

Author

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

Examples

Run this code

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(
  .I==.N, "", sprintf("[[%s][%s]]", url, title)
)), collapse="")]

Run the code above in your browser using DataLab