Escape character for Location of char in a string: R lang

205 Views Asked by At

I'm trying to get the location of \ or / in a string. Below is the code I'm attempting:

x <- "<span id=\"ref_12590587_l\">6,803.61</span>_l>"
gregexpr("\\\", x)
which(strsplit(x, "")[[1]]=="\")

My problem is when I attempt these codes in Rstudio, I get a continue prompt, the REPL prompt becomes +. These codes work for other characters.

Why I'm getting the continue prompt, even though the \ is quoted in the inverted quotes?

Edit: corrected the string after comment.

2

There are 2 best solutions below

2
On BEST ANSWER

You have to add another slash (as stribizhev says in the comments). So you're looking for

gregexpr("\\\\", x)

The reason why is that the you need to escape the \, twice. So \\ gives you only 1 backslash. When you put 3 in, the 3rd backslash is actually escaping the quote!

See for an example:

gregexpr("\"", 'hello, "hello"')

This is searching for the quote in the string.

0
On

Just to formalize my comments:

  1. Your x variable does not contain any backslashes, these are escaping characters that allow us putting literal quotation marks into a string.
  2. gregexpr("\\\", x) contains a non-closed string literal because the quotation mark on the right is escaped, and thus is treated as a literal quotation mark, not the one that is used to "close" a string literal.
  3. To search for a literal \ in gregexpr, we need 4 backslashes \\\\, as gregexpr expects a regular expression. In regular expressions, "\" is a special symbol, thus it must be escaped for the regex engine. But inside gregexpr, we pass a string that itself is using \ for escaping entities like \n. So, we need to escape the backslash for R first, and then for the regex engine.

That said, you can use

gregexpr("\\\\", x) 

to get only literal backslashes, or

gregexpr("\\\\|/", x)

to also look for forward slashes.

See IDEONE demo