R grep finds multiple complete words in a vector

I have a vector of strings, say:

vect<-c("oxidor magnesio","oxido magnesio","oxido calcio", "oxidante","oxido calcio magnesio","magnesio oxido")

      

I would like to find occurrences of both words, "oxido"

and "magnesio"

. What i do is

intersect(grep("\\boxido\\b",vect),grep("\\bmagnesio\\b",vect))

      

But,

  • Question 1 : is there any direct grep command to achieve it?
  • Question 2 : Suppose I want to find occurrences of both words, but in a specific order (say, for example, "oxide" followed by "magnesio", so the correct answer is 2

    and 5

    ). Which team?

Thank,

+3


source to share


1 answer


Edit. Answer 1: I know I am grepl

capable of this, for example:

> grepl("(?=.*\\boxido\\b)(?=.*\\bmagnesio\\b)", vect, perl = TRUE)
[1] FALSE  TRUE FALSE FALSE  TRUE  TRUE

      



Answer 2:

> grep("\\boxido\\b.*\\bmagnesio\\b",vect,v=T)
[1] "oxido magnesio"        "oxido calcio magnesio"

      

+5


source







All Articles