Remove spaces between letters [A-Za-z]

How to remove spaces between letters NOT numbers

For instance:

Enter

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 I couldn’t reach the conclusion I was hoping for

+4
source share
2 answers

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

Watch the R demo online and the regex demo .

NOTE. ignore.case = TRUEmake case insensitive; if not expected, remove this argument.

More details

  • (?<=[A-Z])(or (?<=\p{L})) - the letter should appear immediately to the left of the current position (without adding it to the match)
  • \\s+ - 1 or more spaces
  • (?=[A-Z])(or (?=\\p{L})) - the letter should appear immediately to the right of the current location (without adding it to the correspondence).
+3

gsub, " " "" , .

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"

@Wiktor Stribiżew ( [A-z] [a-zA-Z]):

[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"
+6

Source: https://habr.com/ru/post/1686872/


All Articles