将多个空格合并到单个空间; 删除尾随/前导空格

cha*_*esh 56 r pattern-matching

我想将多个空格合并到单个空格中(空格也可以是制表符)并删除尾随/前导空格.

例如...

string <- "Hi        buddy        what's up    Bro" 
Run Code Online (Sandbox Code Playgroud)

"Hi buddy what's up bro"
Run Code Online (Sandbox Code Playgroud)

我检查了Regex给出的解决方案,用一个空格替换多个空格.请注意,不要将\ t或\n作为玩具字符串中的精确空间并将其作为模式输入gsub.我希望在R.

请注意,我无法在玩具串中放置多个空格.谢谢

Ric*_*ven 55

这似乎符合您的需求.

string <- "  Hi buddy   what's up   Bro "
library(stringr)
str_replace(gsub("\\s+", " ", str_trim(string)), "B", "b")
# [1] "Hi buddy what's up bro"
Run Code Online (Sandbox Code Playgroud)

  • @chandresh - `\\ s +`表示"一个或多个空格" (6认同)
  • 值得注意的是,这是解决将"Bro"中的大写字母b更改为小写的唯一答案,如问题的期望结果所示. (2认同)

Tyl*_*ker 35

使用单个正则表达式的另一种方法:

gsub("(?<=[\\s])\\s*|^\\s+|\\s+$", "", string, perl=TRUE)
Run Code Online (Sandbox Code Playgroud)

解释(来自)

NODE                     EXPLANATION
--------------------------------------------------------------------------------
  (?<=                     look behind to see if there is:
--------------------------------------------------------------------------------
    [\s]                     any character of: whitespace (\n, \r,
                             \t, \f, and " ")
--------------------------------------------------------------------------------
  )                        end of look-behind
--------------------------------------------------------------------------------
  \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
 |                        OR
--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  \s+                      whitespace (\n, \r, \t, \f, and " ") (1 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
Run Code Online (Sandbox Code Playgroud)


Hen*_*rik 28

或者只是尝试一下这个squish功能stringr

library(stringr)
string <- "  Hi buddy   what's up   Bro "
str_squish(string)
# [1] "Hi buddy what's up Bro"
Run Code Online (Sandbox Code Playgroud)


Ada*_*son 10

您无需导入外部库即可执行此任务:

string <- " Hi        buddy        what's up    Bro "
string <- gsub("\\s+", " ", string)
string <- trimws(string)
string
[1] "Hi buddy what's up Bro"
Run Code Online (Sandbox Code Playgroud)

或者,一行:

string <- trimws(gsub("\\s+", " ", string))
Run Code Online (Sandbox Code Playgroud)

干净得多。

  • 这不依赖于任何外部库,也不是像 Tyler Rinker 那样的噩梦般的正则表达式。想知道为什么你没有更多的赞成票吗? (4认同)

Tyl*_*ker 6

qdapRegexrm_white处理这种功能:

library(qdapRegex)
rm_white(string)

## [1] "Hi buddy what's up Bro"
Run Code Online (Sandbox Code Playgroud)