Search code examples
rtidyverse

Count occurrences of a substring in a string


I'm trying to count the number of times a string, say AAA occurs in a longer string, say AAAA. The answer in this case is clearly 2:

  • AAAA
  • AAAA

None of these work as I would expect:

stringi::stri_count(str = "AAAA", regex = "AAA")
[1]

stringr::str_count(string = "AAAA", pattern = "AAA")
[1]

stringr::str_extract_all("AAAA", "AAA", simplify = T)
     [,1]
[1,] "AAA"

What am I doing wrong here?


Solution

  • You could use a look ahead positive regex (?=) to count the occurrence of the substring like this:

    stringi::stri_count(str = "AAAA", regex = "(?=AAA)")
    #> [1] 2
    

    Created on 2023-10-03 with reprex v2.0.2