删除R中的某些破折号

时间:2016-05-10 13:17:49

标签: regex r gsub

我有一个带有几个破折号的字符串。应保留其中一些(字内破折号),其余部分应删除。我设法保持字内短划线并删除大多数字间短划线。但是,保留一个单词开头的短划线。

为什么呢?如何删除破折号?

co <- "keep-this dash but remove - that -----these and these----dashes."
# remove between-word dashes
co <- gsub(" - ", " ", co)
co
# remove multiple dashes
co <- gsub("-{2}", " ", co)
co
# remove special characters but keep intra-word dashes and apostrophes
co <- gsub("[^[:alnum:]['-]", " ", co)
co

1 个答案:

答案 0 :(得分:3)

也许这有帮助

gsub("(?:(-| ))-+\\s*", " ", co, perl=TRUE)
#[1] "keep-this dash but remove that these and these dashes."