Skip to contents

Note that unless you specify an appropriate contents_pattern extract_between_paren will not work as you probably expect if there are multiple sets of parentheses. You can use exclusion patterns to make this work better (e.g. content_pattern = '[^)]*').

Usage

extract_between_paren(
  x,
  left = "\\(",
  right = "\\)",
  contents_pattern = ".*"
)

extract_all_between_paren(
  x,
  left = "\\(",
  right = "\\)",
  contents_pattern = ".*",
  max_iters = 100
)

Arguments

x

Character vector

left

Left parenthetical string

right

Right parenthetical string

contents_pattern

Regex pattern for the contents between parentheses

max_iters

maximum number of items to return

Value

Character vector with NA's for elements in x that do not have parentheses and the substring between the first matching parentheses.

Examples

x = c("-", "", NA, "1", "3", "1 (Alta.)", "(Sask) 20")
extract_between_paren(x)
#> [1] NA      NA      NA      NA      NA      "Alta." "Sask"