Reputation: 1683
Is there a way to pass two terms to grep for search for?
Imagine I wanted to find items in an object containing both "five" and "six" i.e "five" AND "six" .
grep("five" & "six", object)
This will not work, I know there is notation for OR with |.
Upvotes: 3
Views: 1627
Reputation: 62003
You could use grepl
to figure out which elements meet both criteria and then index those out.
id <- grepl("five", object) & grepl("six", object)
object[id]
Upvotes: 7
Reputation: 70750
Lookahead is not needed to do this, you can use the alternation operator in context.
grep('five.*six|six.*five', object)
If you know that "five" will precede "six" on the line, you don't even need the alternatives:
grep('five.*six', object)
Upvotes: 7
Reputation: 174874
You need to use a positive lookahead assertion based regex. Since lookarounds are PCRE feature, you must need to set perl=TRUE
.
grep("^(?=.*five)(?=.*six)", object, perl=TRUE)
OR
grep("^(?=.*\\bfive\\b)(?=.*\\bsix\\b)", object, perl=TRUE)
OR
grep("^(?=.*\\bfive\\b).*\\bsix\\b", object, perl=TRUE)
Example:
> x <- c("five six bar", "five", "six")
> grep("^(?=.*\\bfive\\b)(?=.*\\bsix\\b)", x, perl=TRUE)
[1] 1
Upvotes: 5