match_parts: Partial matches

Description Usage Arguments Details Value See Also Examples

View source: R/coding.R

Description

Helper function to collect partial (or complete) matches of (Perl-compatible) regular expressions.

Usage

1
  match_parts(x, pattern, ignore.case = FALSE)

Arguments

x

Character vector. If it has names, these names will be used as row names or vector names in the output.

pattern

Character scalar with regular expression that defines one to several capturing groups (partial matches) in parentheses. If they are named, these names will be used as column names in the output. Alternatively, if no capturing groups are present and thus no partial matches occur, the complete matches are returned as vector.

ignore.case

Logical scalar passed to regexpr.

Details

For discarding regular expression groups in the output, use non-capturing groups such as (?:left|right).

Value

If capturing groups are used, a matrix of mode ‘character’ with the number of rows equal to the length of x and one column per capturing group. If these were named, their names appear as column names. If no capturing groups were present, a character vector of the same length as x is returned, containing the complete matches, if any. Non-matches are always represented as NA.

See Also

base::regmatches

Examples

1
2
3
4
x <- structure(letters, names = LETTERS)
(m <- match_parts(x, "(?<pos1>.)"))
stopifnot(m == letters,
  colnames(m) == "pos1", rownames(m) == LETTERS)

pkgutils documentation built on May 2, 2019, 5:49 p.m.