Regex 按字符串中的第一个逗号拆分

Regex 按字符串中的第一个逗号拆分,regex,r,Regex,R,如何使用base有效地拆分第一个逗号上的以下字符串 x <- "I want to split here, though I don't want to split elsewhere, even here." strsplit(x, ???) 先谢谢你 编辑:没想到会提到这个。这需要能够概括为一列,字符串向量,如下所示: y <- c("Here's comma 1, and 2, see?", "Here's 2nd sting, like it, not a lot.")

如何使用base有效地拆分第一个逗号上的以下字符串

x <- "I want to split here, though I don't want to split elsewhere, even here."
strsplit(x, ???)
先谢谢你

编辑:没想到会提到这个。这需要能够概括为一列,字符串向量,如下所示:

y <- c("Here's comma 1, and 2, see?", "Here's 2nd sting, like it, not a lot.")
y
库(stringr)

str_sub(x,end=min(str_locate(string=x',,')-1))

这会得到你想要的第一点。更改
stru sub
中的
start=
end=
,以获得所需的其他内容

例如:

str_sub(x,start=min(str_locate(string=x',,')+1))

并用
str_trim
换行以去掉前导空格:


str_trim(str_sub(x,start=min(str_locate(string=x,,)+1))
我可能会这么做。它可能看起来很粗糙,但由于
sub()
strsplit()
都是矢量化的,因此当传递多个字符串时,它也能顺利工作

XX <- "SoMeThInGrIdIcUlOuS"
strsplit(sub(",\\s*", XX, x), XX)
# [[1]]
# [1] "I want to split here"                               
# [2] "though I don't want to split elsewhere, even here."

XX这很有效,但我更喜欢Josh Obrien的:

y <- strsplit(x, ",")
sapply(y, function(x) data.frame(x= x[1], 
    z=paste(x[-1], collapse=",")), simplify=F))

y来自
stringr
软件包:

str_split_fixed(x, pattern = ', ', n = 2)
#      [,1]                  
# [1,] "I want to split here"
#      [,2]                                                
# [1,] "though I don't want to split elsewhere, even here."

(这是一个一行两列的矩阵。)

这里是另一个解决方案,使用正则表达式捕获第一个逗号前后的内容

x <- "I want to split here, though I don't want to split elsewhere, even here."
library(stringr)
str_match(x, "^(.*?),\\s*(.*)")[,-1] 
# [1] "I want to split here"                              
# [2] "though I don't want to split elsewhere, even here."

x非常粗糙,但是像
list(head(y[[1]],1),paste(tail(y[[1]],-1),collapse=“,”)这样的东西怎么样呢?
其中
y
是strsplit(x,…)
的输出。Chase我尝试了它,但似乎无法对类似字符串的向量起作用。我编辑了我的原始帖子来进一步解释这个问题。
str_locate_all(string=y,,)
将找到模式的所有索引位置(在您的例子中是逗号),然后可以应用这些位置从向量或列中进行选择。@josh obrien您如何扩展该代码来修剪[2]中的前导空格。我将用
gsub(^\\s+\\s+$)来包装它我喜欢它,JOSH。它可以工作,非常简单,并且保持在底部。多谢各位+1您可以用
any(grepl(XX,x))
检查您的
XX
是否正常。如果它
FALSE
,那么它就可以了。@Establed1969——要修剪逗号后面的空格,我会做
strsplit(sub(“,\\s*”,XX,x),XX)
。在你的第一部分中,我不明白为什么你会用sapply代替y。您看起来并不需要显式地使用索引。看起来您正在删除所有逗号,即使您希望它们保留在其他字符串中?
str_split_fixed(x, pattern = ', ', n = 2)
#      [,1]                  
# [1,] "I want to split here"
#      [,2]                                                
# [1,] "though I don't want to split elsewhere, even here."
x <- "I want to split here, though I don't want to split elsewhere, even here."
library(stringr)
str_match(x, "^(.*?),\\s*(.*)")[,-1] 
# [1] "I want to split here"                              
# [2] "though I don't want to split elsewhere, even here."