我想替换除第一个连续点之外的所有点。这是我想要的一个例子:
> names.orig <- c("test & best", "test & worse &&&& ? do")
> names <- make.names(names.orig)
> names
[1] "test...best" "test...worse.........do"
>
> # But I want this instead:
> # [1] "test.best" "test.worse.do"
>
> # Desperatley tried:
> gsub("\\.{2, }", "", names)
[1] "testbest" "testworsedo"
> gsub("\\G((?!^).*?|[^\\.]*\\.*?)\\.", "", names)
Error in gsub("\\G((?!^).*?|[^\\.]*\\.*?)\\.", "", names) :
invalid regular expression '\G((?!^).*?|[^\.]*\.*?)\.', reason 'Invalid regexp'
> # etc.
>
> # The only thing that works for me is this
> unlist(lapply(strsplit(names, "\\."), function(x) paste(x[x != ""], collapse=".")))
[1] "test.best" "test.worse.do"
>
> # But, really, what is the right regex in combination with what?
如何用正则表达式解决这个问题?