Is it possible to use an AND operator in grepl()? Is it possible to use an AND operator in grepl()? r r

Is it possible to use an AND operator in grepl()?


To search for a string having both "a" and "b"

grepl("a", x) & grepl("b", x)

or

grepl("a.*b|b.*a", x)

If we know the order of the two parts then we can omit one of the two halves of the last grepl. For example, in the case of the query in the question this would be sufficient to find a string that starts with 55 and contains Roof

grepl("^55.*Roof", x)


I am reposting an answer by @Psidom as his was deleted but was scalable to n AND clauses. If @Psidom undeletes his response I will delete this one (I voted to undelete) but feel the answer is important for other searchers:

s <- c("55 - Roof Structure", "55-Wall to Roof", 'd 55 Roof')grepl("(?=.*^55)^Roof", s, perl = TRUE)## [1]  TRUE  TRUE FALSE## 2 AND clausesgrepl("^(?=.*^55)(?=.*Roof)(?=.*Wall)", s, perl = TRUE)## [1]  FALSE  TRUE FALSE