删除 R 中的某些破折号

Remove certain dashes in R

我有一个包含多个破折号的字符串。其中一些(单词内的破折号)应该保留,其余的应该删除。我设法保留了词内破折号并删除了大部分词间破折号。但是,保留单词开头的破折号。

为什么?我怎样才能删除破折号?

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

也许这有帮助

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