红颜莎娜
对不起,这是一个附加的回答,但这是太多的评论行。我只想提醒你,可以通过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)