R:查找包含多个字符串匹配的向量元素

时间:2017-10-23 16:07:19

标签: r regex

我想找到包含由另一个向量指定的所有字符串的向量(字符串)的元素。例如,

x <- c("xxxabcxdxexfxx", "xxaxbcdexx", "xaxxxbc")
a <- c("a", "b", "c", "d", "e", "f")

我想找到包含x中所有字符串的a元素,以便获取

[1] TRUE FALSE FALSE

3 个答案:

答案 0 :(得分:4)

sapply(x, function(string) all(Vectorize(grepl)(pattern = a, x = string)))
#xxxabcxdxexfxx     xxaxbcdexx        xaxxxbc 
#          TRUE          FALSE          FALSE

OR

rowSums(sapply(a, function(P) grepl(P, x))) == length(a)
#[1]  TRUE FALSE FALSE

OR

grepl(pattern = paste(sort(a), collapse = ""),
      x = sapply(strsplit(x, ""),
                 function(x) paste(sort(x), collapse = "")))
#[1]  TRUE FALSE FALSE

OR

lengths(sapply(strsplit(x,""), setdiff, x = a)) == 0
#[1]  TRUE FALSE FALSE

答案 1 :(得分:3)

另一个:

sapply(strsplit(x,""), function(y) all(a %in% y))

答案 2 :(得分:1)

使用gregexpr

lengths(gregexpr(pattern = paste(a, collapse = "|"), text = x)) == length(a)
# [1]  TRUE FALSE FALSE