一条评论 https://stackoverflow.com/questions/23961022/split-strings-on-first-and-last-commas/23964843?noredirect=1#comment36923487_23964843关于我的回答这个问题 https://stackoverflow.com/a/23964843/1478381 which should使用给出所需的结果strsplit
不,尽管它seems正确匹配字符向量中的第一个和最后一个逗号。这可以证明使用gregexpr
and regmatches
.
那么为什么strsplit
在此示例中按每个逗号进行拆分,即使regmatches
只返回两个匹配项same regex?
# 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] "," ","
啊?!到底是怎么回事?