/ / Sostituisci il contenuto della stringa con un numero arbitrario di quattro caratteri nel mezzo della stringa? - regex, r, string, text, stringr

Sostituisci il contenuto della stringa con un numero arbitrario di quattro caratteri nel mezzo della stringa? - regex, r, string, text, stringr

Obiettivo: trasformare x in y; dove x ha un numero arbitrario di spazi, rs e ns.

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

Ho fatto alcuni tentativi usando str_replace_all ():

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

risposte:

2 per risposta № 1

gsub farà questo lavoro per te.

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

o

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

Esempio:

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