匹配后从文本中提取字符串 - regex R.

Tar*_*rak -5 regex r grepl

我有一个输入文字

   inputQ <- "What can I do ..my baby has rash all over. Suggest good rash cream"
Run Code Online (Sandbox Code Playgroud)

我有一份条款清单

terms <- c("diaper","cloth diaper","rash pants","rash","baby wipes","rash cream")
Run Code Online (Sandbox Code Playgroud)

我希望完全匹配其中一个术语并返回它我尝试使用for循环,但是有更好的方法

结果应该是

 rash cream
Run Code Online (Sandbox Code Playgroud)

存储在matchedTerm中

Cat*_*ath 5

您可以尝试获取所有匹配项,然后检查具有最多字符数的匹配项:

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