Skip to content

Used to save the matched value within the group for use later in the regular expression or to extract the values captured. Both named and unnamed groups can later be referenced using capture_group.

Usage

capture(..., name = NULL)

capture_group(name)

Arguments

...

shortcuts, R variables, text, or other rex functions.

name

of the group. Unnamed capture groups are numbers starting at 1 in the order they appear in the regular expression. If two groups have the same name, the leftmost group is the used in any reference.

See also

group for grouping without capturing. Perl 5 Capture Groups https://perldoc.perl.org/perlre#Capture-groups

Other rex: %or%(), character_class(), counts, group(), lookarounds, not(), rex(), shortcuts, wildcards

Examples


# Match paired quotation marks
re <- rex(
  # first quotation mark
  capture(quotes),

  # match all non-matching quotation marks
  zero_or_more(except(capture_group(1))),

  # end quotation mark (matches first)
  capture_group(1)
)

#named capture - don't match apples to oranges
re <- rex(
  capture(name = "fruit", or("apple", "orange")),
  "=",
  capture_group("fruit")
)