2017-02-17 93 views
-5

我有一個輸入文本提取字符串匹配後 - 正則表達式[R

inputQ <- "What can I do ..my baby has rash all over. Suggest good rash cream" 

我的術語列表

terms <- c("diaper","cloth diaper","rash pants","rash","baby wipes","rash cream") 

我想精確匹配的條件之一,並返回它以及 我嘗試使用for循環,但有沒有更好的方法

結果應該是

rash cream 

存儲在matchedTerm

+1

爲什麼要「疹膏」,而不是「疹「 回?你嘗試了什麼? –

+0

*我希望完全匹配其中一個術語* ...哪一個? – Sotos

+0

可能的重複http://stackoverflow.com/questions/33856148/regex-in-r-extracting-words-from-a-string或http://stackoverflow.com/questions/33784376/extract-word-in-字符串在-R – akrun

回答

5

你可以得到所有的比賽,然後檢查了一個與字符的最大數量:

wh_match <- names(unlist(sapply(terms, grep, inputQ))) 
wh_match[which.max(nchar(wh_match))] 
# [1] "rash cream"