Skip to content

Conversation

yutannihilation
Copy link
Member

Currently, there's no cool way of doing grep -v with stringr. If I want to negate the pattern, I need to use str_detect(), but this is not handy.

library(stringr)

fruit <- c("apple", "banana", "pear", "pinapple")
fruit[!str_detect(fruit, "^p)]

On the other hand, stringi's equivalent functions have negate argument since version 1.1.1. So, it seems easy to introduce the argument in stringr package. Note that my primary motivation was str_subset(), only, but as stringi::stri_detect() also has negate, I included the equivalent functions, str_detect() and str_which().

library(stringr)

fruit <- c("apple", "banana", "pear", "pinapple")
str_subset(fruit, "^p")
#> [1] "pear"     "pinapple"

str_subset(fruit, "^p", negate = TRUE)
#> [1] "apple"  "banana"

Created on 2018-10-01 by the reprex package (v0.2.1)

@hadley hadley merged commit 5177959 into tidyverse:master Jan 4, 2019
@hadley
Copy link
Member

hadley commented Jan 4, 2019

Thanks!

@yutannihilation yutannihilation deleted the add-negate-arg branch January 4, 2019 17:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants