3 回答
TA贡献1815条经验 获得超10个赞
|
grep
or
"001|100|000"
grep
x <- c("1100", "0010", "1001", "1111")pattern <- "001|100|000"grep(pattern, x)[1] 1 2 3
grepl
:
grepl(pattern, x)[1] TRUE TRUE TRUE FALSE
?regex
编辑:paste
:
myValues <- c("001", "100", "000")pattern <- paste(myValues, collapse = "|")
TA贡献1876条经验 获得超7个赞
stringr
require(stringr)mylist = c("1100", "0010", "1001", "1111")str_locate(mylist, "000|001|100")
TA贡献1842条经验 获得超12个赞
对不起,这是一个附加的回答,但这是太多的评论行。
我只想提醒你,可以通过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)
- 3 回答
- 0 关注
- 701 浏览
添加回答
举报