を使用して、さまざまな長さの重複するすべての一致を抽出する方法を次に示しgregexpr
ます。
x<-"abaaaababaaa"
# nest in lookahead + capture group
# to get all instances of the pattern "(ab)|b"
matches<-gregexpr('(?=((ab)|b))', x, perl=TRUE)
# regmatches will reference the match.length attr. to extract the strings
# so move match length data from 'capture.length' to 'match.length' attr
attr(matches[[1]], 'match.length') <- as.vector(attr(matches[[1]], 'capture.length')[,1])
# extract substrings
regmatches(x, matches)
# [[1]]
# [1] "ab" "b" "ab" "b" "ab" "b"
コツは、パターンをキャプチャ グループで囲み、そのキャプチャ グループを先読みアサーションで囲むことです。gregexpr
属性を持つ開始位置を含むリストを返しますcapture.length
。これは、最初の列が最初のキャプチャ グループの一致する長さであるマトリックスです。これをベクトルに変換してmatch.length
属性に移動すると (パターン全体が先読みアサーション内にあるため、すべてゼロです)、 に渡してregmatches
文字列を抽出できます。
最終結果の型からわかるように、いくつかの変更を加えることで、x
が文字列のリストである場合に備えて、これをベクトル化できます。
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"