Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove white spaces between letters [A-Za-z]

Tags:

regex

r

How to remove whitespaces between letters NOT numbers

For example:

Input

I ES P 010 000 000 000 000 000 001 001 000 000 IESP 000 000

Output

IESP 010 000 000 000 000 000 001 001 000 000 IESP 000 000

I tried something like this

gsub("(?<=\\b\\w)\\s(?=\\w\\b)", "", x,perl=T)

But wasn't able to arrive at the output I was hoping for

like image 439
jonnyblue8 Avatar asked Jan 03 '23 09:01

jonnyblue8


2 Answers

Use gsub to replace whitespace " " with nothing "" between letters then return replacement and letters.

Input <- "I ES P 010 000 000 000 000 000 001 001 000 000 IESP 000 000"
gsub("([A-Z]) ([A-Z])", "\\1\\2", Input)
[1] "IESP 010 000 000 000 000 000 001 001 000 000 IESP 000 000"

Edit after @Wiktor Stribiżew comment (replaced [A-z] to [a-zA-Z]):

For lower and upper case use [a-zA-Z]

Input <- "I ES P 010 000 000 000 000 000 001 001 000 000 IESP 000 000 aaa ZZZ"
gsub("([a-zA-Z]) ([a-zA-Z])", "\\1\\2", Input)
[1] "IESP 010 000 000 000 000 000 001 001 000 000 IESP 000 000 aaaZZZ"
like image 124
pogibas Avatar answered Jan 12 '23 07:01

pogibas


You need to use

Input <- "I ES P E ES P 010 000 000 000 000 000 001 001 000 000 IESP 000 000"
gsub("(?<=[A-Z])\\s+(?=[A-Z])", "", Input, perl=TRUE, ignore.case = TRUE)
## gsub("(*UCP)(?<=\\p{L})\\s+(?=\\p{L})", "", Input, perl=TRUE) ## for Unicode

See the R demo online and a regex demo.

NOTE: The ignore.case = TRUE will make the pattern case insensitive, if it is not expected, remove this argument.

Details

  • (?<=[A-Z]) (or (?<=\p{L})) - a letter must appear immediately to the left of the current location (without adding it to the match)
  • \\s+ - 1 or more whitespaces
  • (?=[A-Z]) (or (?=\\p{L})) - a letter must appear immediately to the right of the current location (without adding it to the match).
like image 24
Wiktor Stribiżew Avatar answered Jan 12 '23 07:01

Wiktor Stribiżew