75
string = c("apple", "apples", "applez")
grep("apple", string)

This would give me the index for all three elements in string. But I want an exact match on the word "apple" (i.e I just want grep() to return index 1).

oguz ismail
  • 1
  • 16
  • 47
  • 69
Adrian
  • 9,229
  • 24
  • 74
  • 132

2 Answers2

132

Use word boundary \b which matches a between a word and non-word character,

string = c("apple", "apples", "applez")
grep("\\bapple\\b", string)
[1] 1

OR

Use anchors. ^ Asserts that we are at the start. $ Asserts that we are at the end.

grep("^apple$", string)
[1] 1

You could store the regex inside a variable and then use it like below.

pat <- "\\bapple\\b"
grep(pat, string)
[1] 1
pat <- "^apple$"
grep(pat, string)
[1] 1

Update:

paste("^",pat,"$", sep="")
[1] "^apple$"
string
[1] "apple"   "apple:s" "applez" 
pat
[1] "apple"
grep(paste("^",pat,"$", sep=""), string)
[1] 1
Rich Scriven
  • 97,041
  • 11
  • 181
  • 245
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
  • 5
    `paste0("^",pat,"$")` saves a few characters of typing over `paste`. No need for `sep=""` – Ben Oct 23 '15 at 05:28
43

For exact matching, it makes the most sense to use ==. Additionally, this will be faster than grep(), and is obviously much easier.

which(string == "apple")
# [1] 1
Rich Scriven
  • 97,041
  • 11
  • 181
  • 245