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