Str_count with overlapping substrings

I am trying to count the number of occurrences of a substring inside a character vector. For instance:

lookin<-c("babababa", "bellow", "ra;baba")
searchfor<-"aba"
str_count(lookin, searchfor)

returns: 2 0 1

However, I want it to return “3 0 1”, but it does not rise in the middle of “aba” in the first element, since it is partially used in the first instance (I think).

I found this question , but could not figure out how to use it with a vector that has multiple elements.

+4
source share
1 answer

Try:

str_count(lookin, paste0("(?=",searchfor,")"))

[1] 3 0 1

Which, as stated in your link, uses lookahead to match all instances.

+3
source

Source: https://habr.com/ru/post/1599496/


All Articles