I am trying to get an expression that takes a huge few paragraphs and finds lines with two specific words both in that lines, so I am looking for the AND operator? Any way how to do this?
For example:
c <- ("She sold seashells by the seashore, and she had a great time while doing so.")
I want an expression that finds a line with both "sold" and "great" in the line.
I've tried something like:
grep("sold", "great", c, value = TRUE)
Any ideas?
Thanks so much!
While in most cases, I would go with
stringr
package as already suggested in CPak's answer, there is also i grep solution to this:Hmm, not bad, right? But what if there was a word merely containing the phrase
sold
orgreat
?So you might want to use word boundaries, i.e. match the entire word:
the
\\b
matches first character in the string, last character in the string or between two characters where one belongs to a word and the other does not:More on the
\b
metacharacter here: http://www.regular-expressions.info/wordboundaries.html