我有以下品种的字符串:
A B C Company XYZ Inc S & K Co
我想删除这些字符串中只有1个字母长度的单词之间的空格.例如,在第一个字符串中,我想删除A B和C之间的空格,但不是在C和公司之间.结果应该是:@H_403_4@
ABC Company XYZ Inc S&K Co
在gsub中使用正确的正则表达式是什么?@H_403_4@
这是一种可以做到这一点的方法,混合在一起,而不是一个字的字符…
原文链接:https://www.f2er.com/regex/357156.htmlx <- c('A B C Company','XYZ Inc','S & K Co','A B C D E F G Company') gsub('(?<!\\S\\S)\\s+(?=\\S(?!\\S))','',x,perl=TRUE) # [1] "ABC Company" "XYZ Inc" "S&K Co" "ABCDEFG Company"
说明:@H_403_4@
首先我们断言两个非空格字符不在背靠背之前.然后我们寻找和匹配空白“一个或多个”时间.接下来,我们以前瞻性来断言非空格字符在声明下一个字符不是非空格字符的同时.@H_403_4@
(?<! # 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