I like to know the number of 0
's which are surrounded by 1
. But if there are more than one 0
without interrupted by a 1
it count only as one.
string <- "1101000010101111001110"
This is the closest I'm able to do:
length(gregexpr(pattern ="101",string)[[1]])
Expected output:
5
With gregexpr
you can use lookahead assertion with perl=True
to find overlapping matches:
(?=...)
is a lookahead assertion:
(?=...)
A zero-width positive lookahead assertion. For example,
/\w+(?=\t)/
matches a word followed by a tab, without including the tab in$&
.
length(gregexpr("(?=10+1)", string, perl=TRUE)[[1]])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With