文字ベクトルの最初と最後のコンマに正しく一致しているように見えますが、この質問に対する私の回答に対するコメントは、目的の結果が得られるはずでstrsplit
はありません。これは、 と を使用して証明できます。gregexpr
regmatches
では、同じ正規表現に対して 2 つの一致しか返さstrsplit
ないのに、この例でコンマごとに分割するのはなぜでしょうか?regmatches
# We would like to split on the first comma and
# the last comma (positions 4 and 13 in this string)
x <- "123,34,56,78,90"
# Splits on every comma. Must be wrong.
strsplit( x , '^\\w+\\K,|,(?=\\w+$)' , perl = TRUE )[[1]]
#[1] "123" "34" "56" "78" "90"
# Ok. Let's check the positions of matches for this regex
m <- gregexpr( '^\\w+\\K,|,(?=\\w+$)' , x , perl = TRUE )
# Matching positions are at
unlist(m)
[1] 4 13
# And extracting them...
regmatches( x , m )
[[1]]
[1] "," ","
は?!何が起こっている?