Using grep to identify unique string

r

Solution

Use regular expressions to limit grep to just the letters you want:

grep("^ia$", words) # 2

Problem

Let's say I have a vector string: ``` words <- c("Guardian","ia","librarian") ``` If I grep for "ia", it would return all three. ``` grep("ia",words) # 1 2 3 ``` How can I make it so that it ONLY finds the 2nd term, the one with nothing else in it? Note: I can do the opposite, with something like `grep(".+ia|ia+.",words)` but I'm not sure how to only return the 2nd position in this case.

Original source