Overlapping matches in R

前端 未结 6 1241
不思量自难忘°
不思量自难忘° 2020-12-01 18:22

I have searched and was able to find this forum discussion for achieving the effect of overlapping matches.

I also found the following SO question speaking of findin

6条回答
  •  渐次进展
    2020-12-01 19:14

    An additional answer, based on @hwnd's own answer (the original didn't allow variable-length captured regions), using just built-in R functions:

    > x <- 'ACCACCACCAC'
    > m <- gregexpr('(?=([AC]C))', x, perl=T)[[1]]
    > start <- attr(m,"capture.start")
    > end <- attr(m,"capture.start") + attr(m,"capture.length") - 1
    > sapply(seq_along(m), function(i) substr(x, start[i], end[i]))
    [1] "AC" "CC" "AC" "CC" "AC" "CC" "AC"
    

    Pretty ugly, which is why the stringr etc. packages exist.

提交回复
热议问题