Skip to contents

These functions search for patterns in strings

Usage

str_detect(strings, pattern, fixed = FALSE)

str_detect_starts_with(strings, pattern, fixed = FALSE)

str_detect_ends_with(strings, pattern, fixed = FALSE)

chr_detect_any(strings, pattern, fixed = FALSE)

chr_detect_all(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

str_detect(): A logical vector equal in length to strings, indicating whether the pattern has been found in each of the strings.

str_detect_starts_with() and str_detect_ends_with(): A logical vector equal in length to strings, indicating whether the pattern has been found at the start-of or end-of each strings, respectively.

chr_detect_any(): A single logical value for whether the pattern occurs anywhere in a character vector.

chr_detect_all(): A single logical value for whether the pattern occurs in every element of a character vector.

A logical vector indicating the presence of each pattern in a string.

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/

See also

grepl() which these functions wrap around.

Examples

strings <- c("apple", "banana", "cherry")

str_detect(strings, "a")
#> [1]  TRUE  TRUE FALSE
str_detect_starts_with(strings, "a")
#> [1]  TRUE FALSE FALSE
str_detect_ends_with(strings, "a")
#> [1] FALSE  TRUE FALSE

chr_detect_any(strings, "a")
#> [1] TRUE
chr_detect_all(strings, "a")
#> [1] FALSE