我想替换除了第一个连续点以外的所有点.这是我想要的一个例子:
> 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?
如何用正则表达式解决这个问题?
解决方法
@H_404_18@ 将“”替换为“.”在你的第一个正则表达式: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"