很抱歉将其作为附加答案,但评论的行数过多。
我只是想提醒一下,可以通过paste(..., collapse = "|") 粘贴在一起以用作单个匹配模式的项目数量是有限的 - 见下文。也许有人可以告诉确切的限制在哪里?诚然,这个数字可能不现实,但根据要执行的任务,它不应完全排除在我们的考虑之外。
对于非常多的项目,需要一个循环来检查模式的每个项目。
set.seed(0)
samplefun <- function(n, x, collapse){
paste(sample(x, n, replace=TRUE), collapse=collapse)
}
words <- sapply(rpois(10000000, 8) + 1, samplefun, letters, '')
text <- sapply(rpois(1000, 5) + 1, samplefun, words, ' ')
#since execution takes a while, I have commented out the following lines
#result <- grepl(paste(words, collapse = "|"), text)
# Error in grepl(pattern, text) :
# invalid regular expression
# 'wljtpgjqtnw|twiv|jphmer|mcemahvlsjxr|grehqfgldkgfu|
# ...
#result <- stringi::stri_detect_regex(text, paste(words, collapse = "|"))
# Error in stringi::stri_detect_regex(text, paste(words, collapse = "|")) :
# Pattern exceeds limits on size or complexity. (U_REGEX_PATTERN_TOO_BIG)