str_detect: Detect the presence or absence of a pattern in a string

View source: R/detect.R

str_detectR Documentation

Detect the presence or absence of a pattern in a string

Description

Vectorised over string and pattern, though using vectorised patterns is relatively slow compared to stringr. Equivalent to grepl(pattern, x). See str_which() for an equivalent to grep(pattern, x).

Usage

str_detect(string, pattern, negate = FALSE)

Arguments

string

character vector of strings.

pattern

string or character vector, pattern(s) to match. Can be:

  • A Perl-compatible regular expression (default).

  • Wrap with perl(ignore_case = TRUE) to use case-insensitive matching.

  • Wrap with fixed() to use a fixed/literal match.

  • Wrap with regex() to use a POSIX 1003.2 extended regular expression.

  • Wrap with regex(ignore_case = TRUE) to use case-insensitive matching with a POSIX 1003.2 extended regular expression.

negate

logical, if TRUE return non-matching elements. Default is FALSE.

Value

Returns a logical vector the same length as string.

Examples

fruit <- c("apple", "banana", "pear", "pineapple")
str_detect(fruit, "a")
str_detect(fruit, "^a")
str_detect(fruit, "a$")
str_detect(fruit, "b")
str_detect(fruit, "[aeiou]")

# Also vectorised over pattern
str_detect("aecfg", letters)

# Returns TRUE if the pattern do NOT match
str_detect(fruit, "^p", negate = TRUE)


csdaw/stringrb documentation built on Aug. 13, 2022, 10:55 p.m.