我有一些自然发生的文字:
text="word1 word2 word3. word4, word5 word6 word7"
我希望在该文本中检测到的一些元素:
elements=c("word2","word6 word7",".",",")
然而,
elements[sapply(paste0("\\<",elements,"\\>"),grepl,text)]
只返回unigram&#34; word2&#34;和bigram&#34; word6 word7&#34;。未检测到文本中的句点和逗号。
我怎样才能做到这一点?
答案 0 :(得分:1)
您不需要包含方括号,因为方括号是正则表达式中的特殊元字符,表示字符类。
> text="word1 word2 word3. word4, word5 word6 word7"
> elements=c("word2","word6 word7",".",",")
> elements[sapply(paste0(elements),grepl,text, fixed=T)]
[1] "word2" "word6 word7" "." ","
答案 1 :(得分:0)
elements[sapply(paste0("[",elements,"]"),grepl,text)] does the job.