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.
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.