How to remove whitespaces between letters NOT numbers
如何删除字母NOT数字之间的空格
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
但是无法达到我希望的输出
2 个解决方案
#1
3
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.
在线查看R演示和正则表达式演示。
NOTE: The ignore.case = TRUE
will make the pattern case insensitive, if it is not expected, remove this argument.
注意:ignore.case = TRUE将使模式不敏感,如果不期望,则删除此参数。
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).
(?<= [A-Z])(或(?<= \ p {L})) - 一个字母必须立即出现在当前位置的左侧(不将其添加到匹配项中)
\\ s + - 一个或多个空格
(?= [A-Z])(或(?= \\ p {L})) - 一个字母必须立即出现在当前位置的右侧(不将其添加到匹配中)。
#2
6
Use gsub
to replace whitespace " "
with nothing ""
between letters then return replacement and letters.
使用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"
Edit after @Wiktor Stribiżew comment (replaced [A-z]
to [a-zA-Z]
):
在@WiktorStribiżew评论后编辑(将[A-z]替换为[a-zA-Z]):
For lower and upper case use [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"
#1
3
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.
在线查看R演示和正则表达式演示。
NOTE: The ignore.case = TRUE
will make the pattern case insensitive, if it is not expected, remove this argument.
注意:ignore.case = TRUE将使模式不敏感,如果不期望,则删除此参数。
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).
(?<= [A-Z])(或(?<= \ p {L})) - 一个字母必须立即出现在当前位置的左侧(不将其添加到匹配项中)
\\ s + - 一个或多个空格
(?= [A-Z])(或(?= \\ p {L})) - 一个字母必须立即出现在当前位置的右侧(不将其添加到匹配中)。
#2
6
Use gsub
to replace whitespace " "
with nothing ""
between letters then return replacement and letters.
使用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"
Edit after @Wiktor Stribiżew comment (replaced [A-z]
to [a-zA-Z]
):
在@WiktorStribiżew评论后编辑(将[A-z]替换为[a-zA-Z]):
For lower and upper case use [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"