用正则表达式拆分字符串

时间:2013-03-22 15:19:16

标签: regex r strsplit

我正在寻找一个泛型形式的字符串,其中方括号表示字符串的“部分”。例如:

x <- "[a] + [bc] + 1"

返回一个看起来像的字符向量:

"[a]"  " + "  "[bc]" " + 1"

编辑:结束使用:

x <- "[a] + [bc] + 1"
x <- gsub("\\[",",[",x)
x <- gsub("\\]","],",x)
strsplit(x,",")

4 个答案:

答案 0 :(得分:6)

我已经看过TylerRinker的代码并怀疑它可能比这更清楚,但这可能是学习一组不同功能的方法。 (在我注意到它在空格上分开之前,我更喜欢他。)我尝试将其调整为与strsplit一起使用,但该函数总是删除分隔符。 也许这可以调整为使newstrsplit在分隔符处分裂但留下它们?可能不需要在第一个或最后一个位置拆分并区分开关分离器。

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"

答案 1 :(得分:5)

这是一种懒惰的做法:

FUN <- function(x) {
    all <- unlist(strsplit(x, "\\s+"))
    last <- paste(c(" ", tail(all, 2)), collapse="")
    c(head(all, -2), last)
}

x <- "[a] + [bc] + 1"    
FUN(x)

## > FUN(x)
## [1] "[a]"  "+"    "[bc]" " +1"

答案 2 :(得分:5)

您可以手动计算分割点并使用substring

split.pos <- gregexpr('\\[.*?]',x)[[1]]
split.length <- attr(split.pos, "match.length")
split.start <- sort(c(split.pos, split.pos+split.length))
split.end <- c(split.start[-1]-1, nchar(x))
substring(x,split.start,split.end)
#  [1] "[a]"  " + "  "[bc]" " + 1"

答案 3 :(得分:5)

这是一个在括号上拆分的版本,并使用正向前瞻和后瞻将其保留在结果中:

splitme <- function(x) {
  x <- unlist(strsplit(x, "(?=\\[)", perl=TRUE))
  x <- unlist(strsplit(x, "(?<=\\])", perl=TRUE))
  for (i in which(x=="[")) {
    x[i+1] <- paste(x[i], x[i+1], sep="")
  }
  x[-which(x=="[")]
}
splitme(x)
#[1] "[a]"  " + "  "[bc]" " + 1"