Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove trailing and leading spaces and extra internal whitespace with one gsub call

Tags:

regex

r

I know you can remove trailing and leading spaces with

gsub("^\\s+|\\s+$", "", x)

And you can remove internal spaces with

gsub("\\s+"," ",x)

I can combine these into one function, but I was wondering if there was a way to do it with just one use of the gsub function

trim <- function (x) {
  x <- gsub("^\\s+|\\s+$|", "", x)
  gsub("\\s+", " ", x)
}

testString<- "  This is a      test. "

trim(testString)
like image 914
C_Z_ Avatar asked Jun 10 '15 17:06

C_Z_


2 Answers

Here is an option:

gsub("^ +| +$|( ) +", "\\1", testString)  # with Frank's input, and Agstudy's style

We use a capturing group to make sure that multiple internal spaces are replaced by a single space. Change " " to \\s if you expect non-space whitespace you want to remove.

like image 133
BrodieG Avatar answered Oct 13 '22 02:10

BrodieG


Using a positive lookbehind :

gsub("^ *|(?<= ) | *$",'',testString,perl=TRUE)
# "This is a test."

Explanation :

## "^ *"     matches any leading space 
## "(?<= ) "    The general form is (?<=a)b : 
             ## matches a "b"( a space here)
             ## that is preceded by "a" (another space here)
## " *$"     matches trailing spaces 
like image 38
agstudy Avatar answered Oct 13 '22 00:10

agstudy