代码之家  ›  专栏  ›  技术社区  ›  frank

在单个字符串r中匹配多个模式[重复]

  •  0
  • frank  · 技术社区  · 6 年前

    我想看看,如果 "001" "100" "000" 在4个字符的字符串中发生 0 1 . 例如,4个字符的字符串可以是 "1100" "0010" "1001" "1111" . 如何用一个命令匹配一个字符串中的多个字符串?

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

    0 回复  |  直到 7 年前
        1
  •  40
  •   zx8754    8 年前

    是的,你可以。这个 | 在一个 grep 图案与 or . 所以你可以用 "001|100|000" 作为你的模式。同时, 格雷普 是矢量化的,所以所有这些都可以一步完成:

    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 有关r中正则表达式的帮助。


    编辑: 为了避免手动创建模式,我们可以使用 paste :

    myValues <- c("001", "100", "000")
    pattern <- paste(myValues, collapse = "|")
    
        2
  •  7
  •   Ramnath    14 年前

    下面是一个使用 stringr 包裹

    require(stringr)
    mylist = c("1100", "0010", "1001", "1111")
    str_locate(mylist, "000|001|100")
    
        3
  •  3
  •   Patrick Dignan    14 年前

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

    echo '1100' | grep -e '001' -e '110' -e '101'
    
        4
  •  2
  •   Manuel Bickel    7 年前

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

    我只是想提醒一下,可以通过 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)
    
        5
  •  1
  •   rafa.pereira    8 年前

    您也可以使用 %like% 接线员来自 data.table 图书馆。

    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
    
        6
  •  1
  •   bartektartanus    8 年前

    如果你想要逻辑向量,那么你应该检查 stri_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