Home > OS >  how to use gsub for "\\n\\n" correctly?
how to use gsub for "\\n\\n" correctly?

Time:11-11

Using gsub(), I want to replace "\n\n" to "" in this text:

mystring<-"People Storm Seonul Airport as Citizens Leave\\n\\nCHOI JEONGHO\\n\\nSEOUL, South Korea, Jan. 31"

But, when I ran gsub("[\r\\n]", " ", mystring), it deletes all n in the text.

The output was:

"People Storm Seo ul  Airport as Citize s Leave    CHOI JEO GHO    SEOUL, South Korea, Ja . 31"

Could you teach me why it is so and how to delete only "\n\n"?

CodePudding user response:

  1. You don’t want to use brackets because you’re trying to match a sequence of characters — brackets will match any of the characters individually, which is how you end up with no ns.
  2. To match a literal \\n, you need to escape both backslashes, giving you \\\\n.
gsub("(\r)|(\\\\n)", " ", mystring)

# "People Storm Seonul Airport as Citizens Leave  CHOI JEONGHO  SEOUL, South Korea, Jan. 31"

CodePudding user response:

Here's an option with the stringr package. In R, you need to escape backslashes using \\, so you'll need to use \\\\n as your argument:

stringr::str_remove_all(mystring, "\\\\n")

"People Storm Seonul Airport as Citizens LeaveCHOI JEONGHOSEOUL, South Korea, Jan. 31"
  • Related