加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

正则表达式 – 删除一定长度的单词之间的空格

发布时间:2020-12-14 06:29:28 所属栏目:百科 来源:网络整理
导读:我有以下品种的字符串: A B C CompanyXYZ IncS K Co 我想删除这些字符串中只有1个字母长度的单词之间的空格.例如,在第一个字符串中,我想删除A B和C之间的空格,但不是在C和公司之间.结果应该是: ABC CompanyXYZ IncSK Co 在gsub中使用正确的正则表达式是什
我有以下品种的字符串:
A B C Company
XYZ Inc
S & K Co

我想删除这些字符串中只有1个字母长度的单词之间的空格.例如,在第一个字符串中,我想删除A B和C之间的空格,但不是在C和公司之间.结果应该是:

ABC Company
XYZ Inc
S&K Co

在gsub中使用正确的正则表达式是什么?

这是一种可以做到这一点的方法,混合在一起,而不是一个字的字符…
x <- c('A B C Company','XYZ Inc','S & K Co','A B C D E F G Company')
gsub('(?<!SS)s+(?=S(?!S))','',x,perl=TRUE)
# [1] "ABC Company"     "XYZ Inc"         "S&K Co"          "ABCDEFG Company"

说明:

首先我们断言两个非空格字符不在背靠背之前.然后我们寻找和匹配空白“一个或多个”时间.接下来,我们以前瞻性来断言非空格字符在声明下一个字符不是非空格字符的同时.

(?<!        # look behind to see if there is not:
  S        #   non-whitespace (all but n,r,t,f,and " ")
  S        #   non-whitespace (all but n,and " ")
)           # end of look-behind
s+         # whitespace (n,and " ") (1 or more times)
(?=         # look ahead to see if there is:
  S        #   non-whitespace (all but n,and " ")
  (?!       #   look ahead to see if there is not:
    S      #     non-whitespace (all but n,and " ")
  )         #   end of look-ahead
)           # end of look-ahead

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读