我试图在字符串中获取\
或/
的位置。以下是我尝试的代码:
x <- "<span id=\"ref_12590587_l\">6,803.61</span>_l>"
gregexpr("\\\", x)
which(strsplit(x, "")[[1]]=="\")
我的问题是当我在Rstudio中尝试这些代码时,我得到一个继续提示,REPL提示变为+
。这些代码适用于其他角色。
为什么我会收到继续提示,即使引号中引用了\
?
编辑:在评论后更正了字符串。
答案 0 :(得分:1)
你必须添加另一个斜杠(正如stribizhev在评论中所说)。所以你正在寻找
gregexpr("\\\\", x)
原因是你需要两次逃离\
。所以\\
只给你1个反斜杠。当你输入3时,第3个反斜杠实际上是在逃避引用!
参见示例:
gregexpr("\"", 'hello, "hello"')
这是在字符串中搜索引号。
答案 1 :(得分:1)
Just to formalize my comments:
x
variable does not contain any backslashes, these are escaping characters that allow us putting literal quotation marks into a string.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.\
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.