Dav*_*ger 5 regex r piping stringr
如何为在管道操作中不包含单词的元素的向量进行子集化?(我真的很喜欢管道)
我希望有一些方法可以反转str_subset.在下面的示例中,我只想返回第二个元素x而不是元素hi:
library(stringr)
x <- c("hi", "bye", "hip")
x %>%
str_dup(2) %>% # just an example operation
str_subset("hi") # I want to return the inverse of this
Run Code Online (Sandbox Code Playgroud)
您可以使用^(?!.*hi)断言字符串不包含hi; 正则表达式使用负向前看?!并断言字符串不包含模式.*hi:
x %>%
str_dup(2) %>% # just an example operation
str_subset("^(?!.*hi)")
# [1] "byebye"
Run Code Online (Sandbox Code Playgroud)
或者通过倒车过滤str_detect:
x %>%
str_dup(2) %>% # just an example operation
{.[!str_detect(., "hi")]}
# [1] "byebye"
Run Code Online (Sandbox Code Playgroud)