user124123
user124123

Reputation: 1683

Grep in R using AND

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

Answers (3)

Dason
Dason

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

hwnd
hwnd

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

Avinash Raj
Avinash Raj

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

Related Questions