gpt4 book ai didi

regex - 如何使用删除字符向量内单词之间的多余空白?

转载 作者:行者123 更新时间:2023-12-03 11:36:55 27 4
gpt4 key购买 nike

假设我有一个像这样的字符 vector

"Hi,  this is a   good  time to   start working   together.". 

我只想有
" Hi, this is a good time to start working together." 

两个字之间只有一个空格。我应该如何在R中执行此操作?

最佳答案

gsub是您的 friend :

test <- "Hi,  this is a   good  time to   start working   together."
gsub("\\s+"," ",test)
#[1] "Hi, this is a good time to start working together."
\\s+将匹配任何空格字符(空格,制表符等)或空格字符的重复,并将其替换为单个空格 " "

关于regex - 如何使用删除字符向量内单词之间的多余空白?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19128327/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com