Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
794 views
in Technique[技术] by (71.8m points)

regex - Remove spaces between words of a certain length

I have strings of the following variety:

A B C Company
XYZ Inc
S & K Co

I would like to remove the spaces in these strings that are only between words of 1 letter length. For example, in the first string I would like to remove the spaces between A B and C but not between C and Company. The result should be:

ABC Company
XYZ Inc
S&K Co

What is the proper regex expression to use in gsub for this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here is one way you could do this seeing how & is mixed in and not a word character ...

x <- 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"

Explanation:

First we assert that two non-whitespace characters do not precede back to back. Then we look for and match whitespace "one or more" times. Next we lookahead to assert that a non-whitespace character follows while asserting that the next character is not a non-whitespace character.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...