用字符串中间任意数量的四个字符替换字符串内容?

Replace string content with arbitrary number of four characters in middle of string?

目标:将x变成y;其中 x 有任意数量的空格、\rs 和 \ns。

x <- "some text,                    \r\n                    \r\n)more text"
y <- "some text)more text"

我已经尝试使用 str_replace_all():

str_replace_all(x, "[,][ \r\n][)]", "")
str_replace_all(x, ",[ \r\n])", "")

gsub 会为您完成这项工作。

gsub(",\s*\n\s*\)", ")", s)

gsub(",\s*[\r\n]+\s*\)", ")", s)

示例:

> x <- "some text,                    \r\n                    \r\n)more text"
> gsub(",\s*\n\s*\)", ")", x)
[1] "some text)more text"