Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove spaces between words of a certain length

Tags:

string

regex

r

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?

like image 859
Alex Avatar asked Oct 23 '14 22:10

Alex


People also ask

How do I remove spaces between words in a string?

In Java, we can use regex \\s+ to match whitespace characters, and replaceAll("\\s+", " ") to replace them with a single space.

How do I remove extra spaces from a string?

Use JavaScript's string. replace() method with a regular expression to remove extra spaces. The dedicated RegEx to match any whitespace character is \s .

Does trim remove spaces between words?

The Trim function removes all spaces from a string of text except for single spaces between words.


1 Answers

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 \n, \r, \t, \f, and " ")
  \S        #   non-whitespace (all but \n, \r, \t, \f, and " ")
)           # end of look-behind
\s+         # whitespace (\n, \r, \t, \f, and " ") (1 or more times)
(?=         # look ahead to see if there is:
  \S        #   non-whitespace (all but \n, \r, \t, \f, and " ")
  (?!       #   look ahead to see if there is not:
    \S      #     non-whitespace (all but \n, \r, \t, \f, and " ")
  )         #   end of look-ahead
)           # end of look-ahead
like image 102
hwnd Avatar answered Sep 25 '22 03:09

hwnd