I would like to find all the index of matched pattern in some string. For Example, I have a string x <- "1110001101"
, I want to match it with "11"
, the result should be c(1, 2, 7)
, however, I just can't get 2
...
Method 1: Use
gregexpr
x [1] "1110001101" gregexpr(pattern = "11", x) [[1]] [1] 1 7 # Why isn't there a 2? attr(,"match.length") [1] 2 2 attr(,"useBytes") [1] TRUE
Method 2: Use
str_locate_all
from packagestringr
library(stringr) str_locate_all(pattern = "11", x) [[1]] start end [1,] 1 2 [2,] 7 8 # Why still isn't there a 2?
Did I lose some subtle arguments for these functions? Thanks for your suggestions!