1

Goal: turn x into y; where x has an arbitrary number of spaces, \rs, and \ns.

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

I've made a few attempts using str_replace_all():

str_replace_all(x, "[,][ \r\n][)]", "")
str_replace_all(x, ",[ \r\n])", "")
mef jons
  • 232
  • 1
  • 3
  • 10
  • Arbitrary number of characters ? Do you mean `[add chars here]+` ? Or, like `,[ \r\n]*(?=\))` ? –  Jul 24 '15 at 18:15

1 Answers1

2

gsub will do this job for you.

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

or

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

Example:

> x <- "some text,                    \r\n                    \r\n)more text"
> gsub(",\\s*\\n\\s*\\)", ")", x)
[1] "some text)more text"
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274