问题描述
我希望拆分一个通用形式的字符串,其中方括号表示字符串的部分".例如:
I'm looking to split a string of a generic form, where the square brackets denote the "sections" of the string. Ex:
x <- "[a] + [bc] + 1"
并返回一个如下所示的字符向量:
And return a character vector that looks like:
"[a]" " + " "[bc]" " + 1"
最终使用了这个:
x <- "[a] + [bc] + 1"
x <- gsub("\\[",",[",x)
x <- gsub("\\]","],",x)
strsplit(x,",")
推荐答案
我看过 TylerRinker 的代码,怀疑它可能比这更清楚,但这可能是学习不同功能集的方式.(在我注意到它在空格上分割之前,我更喜欢他.)我尝试将其调整为与 strsplit
一起使用,但该函数总是删除分隔符.也许这可以用来制作一个 newstrsplit
在分隔符处拆分但将它们留在里面?可能不需要在第一个或最后一个位置拆分并区分打开和关闭分隔符.
I've seen TylerRinker's code and suspect it may be more clear than this but this may serve as way to learn a different set of functions. (I liked his better before I noticed that it split on spaces.) I tried adapting this to work with strsplit
but that function always removes the separators.Maybe this could be adapted to make a newstrsplit
that splits at the separators but leaves them in? Probably need to not split at first or last position and distinguish between opening and closing separators.
scan(text= # use scan to separate after insertion of commas
gsub("\\]", "],", # put commas in after "]"'s
gsub(".\\[", ",[", x)) , # add commas before "[" unless at first position
what="", sep=",") # tell scan this character argument and separators are ","
#Read 4 items
#[1] "[a]" " +" "[bc]" " + 1"
这篇关于使用正则表达式拆分字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!