Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 匹配多个模式_R_Regex - Fatal编程技术网

R 匹配多个模式

R 匹配多个模式,r,regex,R,Regex,我想看看“001”或“100”或“000”是否出现在0和1的4个字符的字符串中。例如,4个字符的字符串可能类似于“1100”或“0010”或“1001”或“1111”。如何使用单个命令匹配字符串中的多个字符串 我知道grep可以用于模式匹配,但是使用grep,我一次只能检查一个字符串。我想知道多个字符串是否可以与其他命令或grep本身一起使用。使用-e参数添加其他模式: echo '1100' | grep -e '001' -e '110' -e '101' 是的,你可以。grep模式中的|

我想看看
“001”
“100”
“000”
是否出现在
0和
1的4个字符的字符串中。例如,4个字符的字符串可能类似于
“1100”
“0010”
“1001”
“1111”
。如何使用单个命令匹配字符串中的多个字符串


我知道grep可以用于模式匹配,但是使用grep,我一次只能检查一个字符串。我想知道多个字符串是否可以与其他命令或grep本身一起使用。

使用-e参数添加其他模式:

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

是的,你可以。
grep
模式中的
|
具有相同的含义。因此,您可以使用
“001 | 100 | 000”
作为您的模式来测试您的模式。同时,
grep
是矢量化的,因此所有这些都可以在一个步骤中完成:

x <- c("1100", "0010", "1001", "1111")
pattern <- "001|100|000"

grep(pattern, x)
[1] 1 2 3
有关R中正则表达式的帮助,请参见
?regex


编辑: 为了避免手动创建图案,我们可以使用
粘贴

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

myValues这里有一个使用
stringr
包的解决方案

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

您还可以使用
数据表
库中的
%like%
运算符

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
库(data.table)
#输入

x如果需要逻辑向量,则应检查
stri\u detect
包中的
stringi
函数。在您的案例中,模式是regex,因此请使用此模式:

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
require(微基准)

test很抱歉,这是一个额外的答案,但是评论的行太多了

我只是想提醒一下,可以通过
粘贴(…,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)
set.seed(0)

很抱歉,我忘了说我想在R中做这件事。无论如何,这很有用。@Davidernburg:-)我从痛苦的经历中学到,在R中没有什么是不可能的。这总是一个如何做的问题!请注意,
%like%
只是
grepl
的包装,请检查
?%like%
“参数:…传递给grepl的模式。”。至少达到
数据。表
版本1.10.4-2。使用perl=T,即结果
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)