使用grep匹配不同可能性的string

6 Solutions collect form web for “使用grep匹配不同可能性的string”

` `x <- c("1100", "0010", "1001", "1111") pattern <- "001|100|000" grep(pattern, x) [1] 1 2 3` `

` `grepl(pattern, x) [1] TRUE TRUE TRUE FALSE` `

` `myValues <- c("001", "100", "000") pattern <- paste(myValues, collapse = "|")` `

` `require(stringr) mylist = c("1100", "0010", "1001", "1111") str_locate(mylist, "000|001|100")` `

` `echo '1100' | grep -e '001' -e '110' -e '101'` `

` `library(data.table) # input x <- c("1100", "0010", "1001", "1111") pattern <- "001|100|000" # check for pattern x %like% pattern > [1] TRUE TRUE TRUE FALSE` `

` `stri_detect_regex(x, pattern) ## [1] TRUE TRUE TRUE FALSE` `

` `require(microbenchmark) test <- stri_paste(stri_rand_strings(100000, 4, "[0-1]")) head(test) ## [1] "0001" "1111" "1101" "1101" "1110" "0110" microbenchmark(stri_detect_regex(test, pattern), grepl(pattern, test)) Unit: milliseconds expr min lq mean median uq max neval stri_detect_regex(test, pattern) 29.67405 30.30656 31.61175 30.93748 33.14948 35.90658 100 grepl(pattern, test) 36.72723 37.71329 40.08595 40.01104 41.57586 48.63421 100` `

` `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)` `
• 在ggplot中使用预定义的调色板
• 有没有办法从JavaScript运行R代码？
• 在tbl_df中包装时查看整个数据框？
• 在data.table中转换列类
• 无效因子水平，NA产生
• 根据列中的公共值将大数据框分割成数据框列表
• C中的滚动中值algorithm
• 在R中设置默认的CRAN镜像
• 因素水平和因素标签之间的混淆
• R：屏蔽function
• 从R中提取POSIXct中的小时和秒作为绘图目的