Overlapping matches in R

前端 未结 6 1250
不思量自难忘°
不思量自难忘° 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:20

    Another roundabout way of extracting the same information that I've done in the past is to replace the "match.length" with the "capture.length":

    x <- c("ACCACCACCAC","ACCACCACCAC")
    m <- gregexpr('(?=([AC]C))', x, perl=TRUE)
    m <- lapply(m, function(i) {
           attr(i,"match.length") <- attr(i,"capture.length")
           i
         })
    regmatches(x,m)
    
    #[[1]]
    #[1] "AC" "CC" "AC" "CC" "AC" "CC" "AC"
    #
    #[[2]]
    #[1] "AC" "CC" "AC" "CC" "AC" "CC" "AC"
    

提交回复
热议问题