我有一个简单的字符串:
y <- "Location 433900E 387200N, Lat 53.381 Lon -1.490, 131 metres amsl"
当我对其执行正则表达式捕获时:
stringr::str_extract(r'Lat(.*?)\,', y)
我收到此错误:
>Error: malformed raw string literal at line 1
为什么?
最佳答案
使用 R 的原始字符串(在版本 4.0.0 中引入),您需要使用 (
或 [
或 {
以及引号,例如,
r'{Lat(.*?)\,}'
这记录在?Quotes
(以及发行说明中):
Raw character constants are also available using a syntax similar to the one used in C++:
r"(...)"
with...
any character sequence, except that it must not contain the closing sequence)"
. The delimiter pairs[]
and{}
can also be used, andR
can be used in place ofr
."
关于r - 字符串中格式错误的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74268079/