I have a line like
'abbb'
I need to understand how many times I can find the substring 'bb'.
grep('bb','abbb')
returns 1. Therefore, the answer is 2(a-bb and ab-bb). How can I count the number of occurrences as I need?
1
2
Here is an ugly approach using substr and sapply:
input <- "abbb" search <- "bb" res <- sum(sapply(1:(nchar(input)-nchar(search)+1),function(i){ substr(input,i,i+(nchar(search)-1))==search }))
You can make the template non-consuming with '(?=bb)', as in:
'(?=bb)'
length(gregexpr('(?=bb)', x, perl=TRUE)[[1]]) [1] 2
stri_count
library(stringi) stri_count_regex(input, '(?=bb)') #[1] 2 stri_count_regex(x, '(?=bb)') #[1] 0 1 0
input <- "abbb" x <- c('aa','bb','ba')
Source: https://habr.com/ru/post/1629949/More articles:SOCKS proxies with Python - djangoIonic application open link in system browser - angularjslaravel: дезинфицировать данные запроса до проверки - validationHow to inherit from jQuery - javascriptChange Charset page using javascript - javascriptIs parseInt () faster than toString ()? - performanceHow to copy TypedArray to another TypedArray? - performanceHow to combine pattern and attribute based routing in ASP.NET Core? - asp.netCan Ruby stones be used with Crystal? - crystal-langЛюбые недостатки использования приложения Qt visual studio - c++All Articles