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).