5

我想替换除第一个连续点之外的所有点。这是我想要的一个例子:

> 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?

如何用正则表达式解决这个问题?

4

1 回答 1

6

在您的第一个正则表达式中替换"""."

R> nms <- make.names(c("test & best", "test & worse &&&&  ? do"))
R> gsub("\\.{2, }", ".", nms)
[1] "test.best"     "test.worse.do"

这也有效。基本上,您用一个点替换所有点和连续点。

R> gsub("\\.+", ".", nms)
[1] "test.best"     "test.worse.do"
于 2013-06-03T01:47:34.787 回答