匹配后从文本中提取字符串 - 正则表达式 R
Extract string from text after matching - regex 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")
我希望与其中一个字词完全匹配 return
我尝试使用for循环,但有没有更好的方法
结果应该是
rash cream
存储在 matchedTerm
您可以尝试获取所有匹配项,然后检查字符数最多的匹配项:
wh_match <- names(unlist(sapply(terms, grep, inputQ)))
wh_match[which.max(nchar(wh_match))]
# [1] "rash cream"
我有一个输入文本
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")
我希望与其中一个字词完全匹配 return 我尝试使用for循环,但有没有更好的方法
结果应该是
rash cream
存储在 matchedTerm
您可以尝试获取所有匹配项,然后检查字符数最多的匹配项:
wh_match <- names(unlist(sapply(terms, grep, inputQ)))
wh_match[which.max(nchar(wh_match))]
# [1] "rash cream"