gregexpr.
x<-"abaaaababaaa"
matches<-gregexpr('(?=((ab)|b))', x, perl=TRUE)
attr(matches[[1]], 'match.length') <- as.vector(attr(matches[[1]], 'capture.length')[,1])
regmatches(x, matches)
, lookahead. gregexpr , capture.length, , . match.length ( , lookahead), regmatches .
As outlined by the type of end result, with several modifications, this can be vectorized for when it xis a list of strings.
x<-list(s1="abaaaababaaa", s2="ab")
matches<-gregexpr('(?=((ab)|b))', x, perl=TRUE)
# make a function that replaces match.length attr with capture.length
set.match.length<-
function(x) structure(x, match.length=as.vector(attr(x, 'capture.length')[,1]))
# set match.length to capture.length for each match object
matches<-lapply(matches, set.match.length)
# extract substrings
mapply(regmatches, x, lapply(matches, list))
# $s1
# [1] "ab" "b" "ab" "b" "ab" "b"
#
# $s2
# [1] "ab" "b"
source
share