r grep by regex - finding a string that contains a sub string exactly one once

前端 未结 6 1894
暗喜
暗喜 2021-01-25 22:10

I am using R in Ubuntu, and trying to go over list of files, some of them i need and some of them i don\'t need,

I try to get the one\'s i need by finding a sub string

6条回答
  •  面向向阳花
    2021-01-25 22:44

    We can use stringi::stri_count:

    library(stringi)
    library(purrr)
    
    # simulate some data
    set.seed(1492)
    (map_chr(1:10, function(i) {
      paste0(sample(letters, sample(10:30), replace=TRUE), collapse="")
    }) -> strings)
    
    ## [1] "jdpcypoizdzvfzs"               "gyvcljnfmrzmdmkufq"           
    ## [3] "xqwrmnklbixnccwyaiadrsxn"      "bwbenawcwvdevmjfvs"           
    ## [5] "ytzwnpkuromfbklfsdnbwwnlrw"    "wclxpzftqgwxyetpsuslgohcdenuj"
    ## [7] "czkhanefss"                    "mxsrqrackxvimcxqcqsditrou"    
    ## [9] "ysqshvzjjmwes"                 "yzawyoqxqxiasensorlenafcbk" 
    
    # How many "w"s in each string?
    stri_count_regex(strings, "w{1}")
    
    ## [1] 0 0 2 3 4 2 0 0 1 1
    

提交回复
热议问题