Learn R Programming

re2 (version 0.1.3)

re2_which: Select strings that match, or find their positions

Description

re2_subset returns strings that match a pattern. re2_which is equivalent to grep(pattern, x). It returns position of string that match a pattern. Vectorized over string and pattern. For the equivalent of grepl(pattern, x) see re2_detect.

Usage

re2_which(string, pattern)

re2_subset(string, pattern)

Value

re2_subset returns a character vector, and re2_which

returns an integer vector.

Arguments

string

A character vector, or an object which can be coerced to one.

pattern

Character string containing a regular expression, or a pre-compiled regular expression (or a vector of character strings and pre-compiled regular expressions).
See re2_regexp for available options.
See re2_syntax for regular expression syntax.

See Also

re2_regexp for options to regular expression, re2_syntax for regular expression syntax, and re2_detect to find presence of a pattern (grep).

Examples

Run this code
color <- c("yellowgreen", "steelblue", "GOLDENROD", "forestgreen")
re2_which(color, "o")
re2_subset(color, "o")

re2_which(c("x", "y", NA, "foo", ""), ".")
re2_subset(c("x", "y", NA, "foo", ""), ".")

# Use precompiled regexp
re <- re2_regexp("[a-z]")
re2_which(color, re)
re2_subset(color, re)

re <- re2_regexp("[a-z]", case_sensitive = FALSE)
re2_which(color, re)
re2_subset(color, re)

# Vector of patterns
re2_which(color, c("^o", "bl.e$", re, "$"))

Run the code above in your browser using DataLab