Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
* `str_interp()` now renders lists consistently independent on the presence of
additional placeholders (@amhrasmussen)

* `str_subset()`, `str_detect()`, and `str_which()` gets `negate` argument,
which is useful when you want the elements that do NOT match (#259,
@yutannihilation).

# stringr 1.3.1

* `str_replace_all()` with a named vector now respects modifier functions (#207)
Expand Down
15 changes: 10 additions & 5 deletions R/detect.r
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#' Match character, word, line and sentence boundaries with
#' [boundary()]. An empty pattern, "", is equivalent to
#' `boundary("character")`.
#'
#' @param negate If `TRUE`, return non-matching elements.
#' @return A logical vector.
#' @seealso [stringi::stri_detect()] which this function wraps,
#' [str_subset()] for a convenient wrapper around
Expand All @@ -35,12 +37,15 @@
#'
#' # Also vectorised over pattern
#' str_detect("aecfg", letters)
str_detect <- function(string, pattern) {
#'
#' # Returns TRUE if the pattern do NOT match
#' str_detect(fruit, "^p", negate = TRUE)
str_detect <- function(string, pattern, negate = FALSE) {
switch(type(pattern),
empty = ,
bound = str_count(string, pattern) > 0,
fixed = stri_detect_fixed(string, pattern, opts_fixed = opts(pattern)),
coll = stri_detect_coll(string, pattern, opts_collator = opts(pattern)),
regex = stri_detect_regex(string, pattern, opts_regex = opts(pattern))
bound = str_count(string, pattern) > 0 & !negate,
fixed = stri_detect_fixed(string, pattern, negate = negate, opts_fixed = opts(pattern)),
coll = stri_detect_coll(string, pattern, negate = negate, opts_collator = opts(pattern)),
regex = stri_detect_regex(string, pattern, negate = negate, opts_regex = opts(pattern))
)
}
17 changes: 10 additions & 7 deletions R/subset.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,24 @@
#' str_subset(fruit, "b")
#' str_subset(fruit, "[aeiou]")
#'
#' # Returns elements that do NOT match
#' str_subset(fruit, "^p", negate = TRUE)
#'
#' # Missings never match
#' str_subset(c("a", NA, "b"), ".")
#' str_which(c("a", NA, "b"), ".")
str_subset <- function(string, pattern) {
str_subset <- function(string, pattern, negate = FALSE) {
switch(type(pattern),
empty = ,
bound = string[str_detect(string, pattern)],
fixed = stri_subset_fixed(string, pattern, omit_na = TRUE, opts_fixed = opts(pattern)),
coll = stri_subset_coll(string, pattern, omit_na = TRUE, opts_collator = opts(pattern)),
regex = stri_subset_regex(string, pattern, omit_na = TRUE, opts_regex = opts(pattern))
bound = string[str_detect(string, pattern) & !negate],
fixed = stri_subset_fixed(string, pattern, omit_na = TRUE, negate = negate, opts_fixed = opts(pattern)),
coll = stri_subset_coll(string, pattern, omit_na = TRUE, negate = negate, opts_collator = opts(pattern)),
regex = stri_subset_regex(string, pattern, omit_na = TRUE, negate = negate, opts_regex = opts(pattern))
)
}

#' @export
#' @rdname str_subset
str_which <- function(string, pattern) {
which(str_detect(string, pattern))
str_which <- function(string, pattern, negate = FALSE) {
which(str_detect(string, pattern, negate = negate))
}
7 changes: 6 additions & 1 deletion man/str_detect.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions man/str_subset.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions tests/testthat/test-detect.r
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ test_that("special cases are correct", {
test_that("vectorised patterns work", {
expect_equal(str_detect("ab", c("a", "b", "c")), c(T, T, F))
expect_equal(str_detect(c("ca", "ab"), c("a", "c")), c(T, F))

# negation works
expect_equal(str_detect("ab", c("a", "b", "c"), negate = TRUE), c(F, F, T))
})

test_that("modifiers work", {
Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/test-subset.r
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,20 @@ test_that("basic subsetting for fixed patterns works", {
str_subset(c("i", "I"), fixed("i", ignore_case = TRUE)),
c("i", "I")
)

# negation works
expect_equal(str_subset(c("i", "I"), fixed("i"), negate = TRUE), "I")
})

test_that("str_which is equivalent to grep", {
expect_equal(
str_which(head(letters), "[aeiou]"),
grep("[aeiou]", head(letters))
)

# negation works
expect_equal(
str_which(head(letters), "[aeiou]", negate = TRUE),
grep("[aeiou]", head(letters), invert = TRUE)
)
})