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

自闭症网瘾萝莉.ら 提交于 2019-11-26 00:49:08

问题


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


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/26813667/how-to-use-grep-gsub-to-find-exact-match

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!