strex
offers easy and/or versions of
stringr::str_detect()
via str_detect_all()
and
str_detect_any()
. These are vectorized over
string
but not pattern
.
stringr::fixed()
and stringr::coll())
are
handled correctly. Otherwise, stringr
regular expressions
are used. For str_detect_all()
, a pattern argument
c("x", "y")
is converted to "(?=.*x)(?=.*y)"
.
For str_detect_any()
, a pattern argument
c("x", "y")
is converted to "x|y"
.
str_detect_all("quick brown fox", c("x", "y", "z"))
#> [1] FALSE
str_detect_all(c(".", "-"), ".")
#> [1] TRUE TRUE
str_detect_all(c(".", "-"), coll("."))
#> [1] TRUE FALSE
str_detect_all(c(".", "-"), coll("."), negate = TRUE)
#> [1] FALSE TRUE
str_detect_all(c(".", "-"), c(".", ":"))
#> [1] FALSE FALSE
str_detect_all(c(".", "-"), coll(c(".", ":")))
#> [1] FALSE FALSE
str_detect_all("xyzabc", c("a", "c", "z"))
#> [1] TRUE
str_detect_all(c("xyzabc", "abcxyz"), c(".b", "^x"))
#> [1] TRUE FALSE
str_detect_any("quick brown fox", c("x", "y", "z"))
#> [1] TRUE
str_detect_any(c(".", "-"), ".")
#> [1] TRUE TRUE
str_detect_any(c(".", "-"), coll("."))
#> [1] TRUE FALSE
str_detect_any(c(".", "-"), coll("."), negate = TRUE)
#> [1] FALSE TRUE
str_detect_any(c(".", "-"), c(".", ":"))
#> [1] TRUE TRUE
str_detect_any(c(".", "-"), coll(c(".", ":")))
#> [1] TRUE FALSE
str_detect_any(c("xyzabc", "abcxyz"), c(".b", "^x"))
#> [1] TRUE TRUE
Unless you’re doing a huge amount of computation, it won’t matter,
but FWIW, it’s faster to convert to regex using
str_escape()
rather than using coll()
.
bench::mark(
str_detect_all(rep("*", 1000), rep(str_escape("*"), 555)),
str_detect_all(rep("*", 1000), coll(rep("*", 555))),
min_iterations = 100
)
#> # A tibble: 2 × 6
#> expression min median `itr/sec` mem_alloc `gc/sec`
#> <bch:expr> <bch> <bch:> <dbl> <bch:byt> <dbl>
#> 1 "str_detect_all(rep(\"*\", 1000), r… 178ms 195ms 5.04 167.18KB 0
#> 2 "str_detect_all(rep(\"*\", 1000), c… 164ms 173ms 5.78 4.31MB 0.642