且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用gsub删除多个逗号和尾部逗号

更新时间:2023-02-18 17:21:43

解决方案不是很相似吗?

Isn't the solution pretty similar?

x <- c("a,b,c", ",a,b,,c", ",,,a,,,b,c,,,")
gsub("^,*|(?<=,),|,*$", "", x, perl=T)
# [1] "a,b,c" "a,b,c" "a,b,c"

正则表达式^,*|(?<=,),|,*$分为三个部分:

There are three parts to the regex ^,*|(?<=,),|,*$:

  • ^,*-匹配字符串开头的0个或多个逗号
  • (?<=,),-这是正向后看,以查看是否有逗号在逗号后面,因此它与,,
  • 中的,相匹配
  • ,*$-匹配字符串末尾的0个或多个逗号
  • ^,* -- this matches 0 or more commas at the beginning of the string
  • (?<=,), -- this is a positive lookbehind to see if there a comma behind a comma, so it matches , in ,,
  • ,*$ -- this matches 0 or more commas at the end of the string

如您所见,以上所有内容均被替换为空.

As you can see all of the above are substituted with nothing.

您可以使用以下功能将此通用名称应用于任何字符(" "","等):

You can make this generic to any character (" ", ",", etc.) with this function:

TrimMult <- function(x, char=" ") {
  return(gsub(paste0("^", char, "*|(?<=", char, ")", char, "|", char, "*$"),
              "", x, perl=T))
}