How to use grep()/gsub() to find exact match

RRegexWord Boundary

R Problem Overview

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

R Solutions

Solution 1 - R

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


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


paste("^",pat,"$", sep="")
[1] "^apple$"
[1] "apple"   "apple:s" "applez" 
[1] "apple"
grep(paste("^",pat,"$", sep=""), string)
[1] 1

Solution 2 - R

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


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAdrianView Question on Stackoverflow
Solution 1 - RAvinash RajView Answer on Stackoverflow
Solution 2 - RRich ScrivenView Answer on Stackoverflow