Skip to contents

chr_subset returns a subset of the input character vector, containing only the elements that match a specified pattern.

Usage

chr_subset(strings, pattern, fixed = FALSE)

Arguments

strings

A character vector, where each element of the vector is a character string.

pattern

A single character string to be searched for in each element of strings. By default, pattern is interpreted as a regular expression (regex). If the fixed argument is set to TRUE, pattern will be treated as a literal string to be matched exactly.

fixed

Logical; whether pattern should be matched exactly, treating regex special characters as regular string characters. Default FALSE.

Value

A character vector containing only the elements of strings that match the specified pattern. If no matches are found, an empty character vector is returned.

Details

These functions are built using the base R regular expression functions. {suitestrings} uses Perl-compatible Regular Expressions (PCRE). This is achieved by setting perl = TRUE in the underlying base functions. See R's regexp documentation for info on the regex implementation. For complete syntax details see https://www.pcre.org/current/doc/html/

Examples

chr_subset(c("apple", "banana", "cherry", "date"), "a")
#> [1] "apple" "banana" "date"
chr_subset(c("apple", "banana", "cherry", "date"), "^a")
#> [1] "apple"