/ / Substituir o conteúdo da string por um número arbitrário de quatro caracteres no meio da string? - regex, r, string, texto, stringr

Substituir o conteúdo da string por um número arbitrário de quatro caracteres no meio da string? - regex, r, string, texto, stringr

Objetivo: transformar x em y; onde x tem um número arbitrário de espaços, rs e ns.

x <- "some text,                    rn                    rn)more text"
y <- "some text)more text"

Eu fiz algumas tentativas usando str_replace_all ():

str_replace_all(x, "[,][ rn][)]", "")
str_replace_all(x, ",[ rn])", "")

Respostas:

2 para resposta № 1

gsub vai fazer esse trabalho para você.

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

ou

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

Exemplo:

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