Regex match exact number of letters

后端 未结 4 951
离开以前
离开以前 2020-12-21 15:51

Let\'s say I want to find all words in which letter \"e\" appears exactly two times. When I define this pattern:

pattern1 <- \"e.*e\" 
grep(pattern1, stri         


        
4条回答
  •  -上瘾入骨i
    2020-12-21 16:36

    If you're okay not using grep

    stringr::str_count(words, "e") == 2
    

    If you want more efficiency,

    stringi::stri_count_fixed(words, "e") == 2
    

    Both of these return logical vectors, you can get the words with words[..code from above..]

提交回复
热议问题