使用gsub删除多个逗号和尾部逗号 [英] Removing multiple commas and trailing commas using gsub
问题描述
这个问题与使用gsub删除多个空格和尾随空格非常相似,但我想申请逗号而不是空格.
This question is very similar to Removing multiple spaces and trailing spaces using gsub, except that I'd like to apply it to commas instead of spaces.
例如,我想要一个函数TrimCommas
将x
转换为y
:
For example, I'd like a function TrimCommas
to turn x
into y
:
x <- c("a,b,c", ",a,b,,c", ",,,a,,,b,c,,,")
# y <- TrimCommas(x) # presumably
y <- c("a,b,c", "a,b,c", "a,b,c")
空格的解决方案是gsub("^ *|(?<= ) | *$", "", x, perl=T)
,所以我希望对此进行比较,以帮助解释一些正则表达式的基本原理.
The solution for spaces was gsub("^ *|(?<= ) | *$", "", x, perl=T)
, so I'm hoping comparing the solution for this will help explain some regex fundamentals as well.
推荐答案
解决方案不是很相似吗?
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))
}
这篇关于使用gsub删除多个逗号和尾部逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!