Detect matching pattern in character vector.

chr_detect(x, pat, ignore.case = FALSE, ...)

Arguments

x

Character vector

pat

Pattern (regex) to detect from text.

ignore.case

Logical indicating whether to ignore capitalization. Defaults to false.

...

Other named arguments passed to grepl or grep See details for more information.

Value

Logical vector indicating whether each element matched the supplied pattern.

Details

This is a wrapper around the base R functions grepl and grep. By default, logical values are returned (a la grepl). To return values, include value = TRUE. To return positions, include which = TRUE, pos = TRUE, or position = TRUE.

Examples

## return logical vector chr_detect(letters, "a|b|c|x|y|z")
#> [1] TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE #> [25] TRUE TRUE
## return inverted logical values chr_detect(letters, "a|b|c|x|y|z", invert = TRUE)
#> [1] FALSE FALSE FALSE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE #> [13] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE FALSE #> [25] FALSE FALSE
## return matching positions chr_detect(letters, "a|b|c|x|y|z", which = TRUE)
#> [1] 1 2 3 24 25 26
## return inverted matching positions chr_detect(letters, "a|b|c|x|y|z", which = TRUE, invert = TRUE)
#> [1] 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
## return matching values chr_detect(letters, "a|b|c|x|y|z", value = TRUE)
#> [1] "a" "b" "c" "x" "y" "z"
## return inverted matching values chr_detect(letters, "a|b|c|x|y|z", value = TRUE, invert = TRUE)
#> [1] "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" #> [20] "w"