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)
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)
干净得多。
在qdapRegex
有rm_white
处理这种功能:
library(qdapRegex)
rm_white(string)
## [1] "Hi buddy what's up Bro"
Run Code Online (Sandbox Code Playgroud)