How to use grep()/gsub() to find exact match
Asked Answered
B

2

80
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).

Bhakti answered 8/11, 2014 at 4:15 Comment(4)
For exact matches, consider using == or match.Ellie
What about for gsub instead of grep?Mord
Combining any and == is better option if you don't need grep arguments such as ignore.case = true or value = trueUpshaw
Related https://mcmap.net/q/260778/-using-regex-in-r-to-find-strings-as-whole-words-but-not-strings-as-part-of-words/680068Alephnull
S
138

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
Schram answered 8/11, 2014 at 4:16 Comment(1)
paste0("^",pat,"$") saves a few characters of typing over paste. No need for sep=""Courland
T
45

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
Thibodeau answered 8/11, 2014 at 4:21 Comment(1)
Since which(string %in% "apple") also works and you mention speed, I would like to know if == is faster than %in%?Zerelda

© 2022 - 2024 — McMap. All rights reserved.