[英]Search special pattern in string: R
I like to know the number of 0
's which are surrounded by 1
. 我想知道被
1
包围的0
的数量。 But if there are more than one 0
without interrupted by a 1
it count only as one. 但是如果有多个
0
没有被1
中断,它只计为一个。
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: 使用
gregexpr
你可以使用perl=True
lookahead断言来查找重叠匹配:
(?=...)
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$&
.例如,
/\\w+(?=\\t)/
匹配单词后跟选项卡,而不包括$&
的选项卡。
length(gregexpr("(?=10+1)", string, perl=TRUE)[[1]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.