regmatches
Extract or Replace Matched Substrings

Description

Extracts or replaces regular expression-matched substrings.

Usage

regmatches(x, m, invert = FALSE)
regmatches(x, m, invert = FALSE) <- value

Arguments

x a character vector.
m an object returned by regexpr, gregexpr, or regexec. It must be the same length as x. Usually it is the result of of regexpr (or like function) operating on x.
invert a logical value. If TRUE, extract or replace the non-matched substrings of x. If FALSE, operate on the matches substrings.
value a character vector to replace the matched or non-matched substrings. It is replicated to the length of x, and its i'th} element is used for all replacements in the \code{i'th element of x.

Details

regmatches() extracts a matched (invert = FALSE) or a non-matched (invert = TRUE) substring from x. The matched data is defined in the object m, which is normally returned by regexpr, gregexpr or regexec.
regmatches()<- replaces the matched (invert = FALSE) or the non-matched (invert = TRUE) substring in x with a new value. The new value must be given in the same type and length of m. NAs are not allowed in the new value.
The m could be supplied as a character vector or a list of character vectors.
Value
regmatches()returns a character vector with matched or non-matched substrings if m is a character vector and invert is FALSE. Otherwise, it returns a list of character vectors with matched or non-matched substrings.
regmatches()<-returns the updated character vector, thus updating its first argument when called in the usual way as regmatches(x, m) <- v.
See Also
regexpr, gregexpr, regexec
Examples
x <- c("Arkansas", "Alabama", "Calabash", "Washington")
pattern <- "[Aa][^a]*a" # sequences starting with A or a and continuing up to next a.

regmatches(x, gregexpr(pattern, x)) regmatches(x, gregexpr(pattern, x), invert = TRUE)

regmatches(x, regexpr(pattern, x)) regmatches(x, regexpr(pattern, x), invert = TRUE)

tmp <- x regmatches(tmp, gregexpr(pattern, tmp)) <- paste0("<",seq_along(x),">") tmp

Package base version 6.0.0-69
Package Index