julia 的 RE 转义貌似有点问题·

首先有神奇的字符串转义

julia> "a\\\"b" == """a\\"b"""
true

julia> "a\\\"b" == """a\\\"b"""
true

看上去 """" 转不转义都行,\ 必须转义。

然后 julia 在处理 re 时就忘了这茬了。

julia> r"""[\\\"]"""
r"[\\"]"

julia> r"[\\"]"
ERROR: syntax: extra token "]" after end of expression

julia> r"""[\\\"]""" == r"[\\\"]" # 正确的转义
true

为啥能发现这个?
因为用 RE 匹配含转义双引号的字符串时匹配不成功。


第一个看上去是 feature

第二个报错应该和这个有关

The exception is that quotation marks still must be escaped. Backslashes
escape both quotation marks and other backslashes, but only when a sequence
of backslashes precedes a quote character.
Thus, 2n backslashes followed by a quote encodes n backslashes and the end of the literal
while 2n+1 backslashes followed by a quote encodes n backslashes followed by a quote character.

这个是 @raw_str 的文档,貌似整个 Julia 代码库里就这里 + history 里提到了这一点
—— @raw_str - julia/io.jl at a0bb006 · JuliaLang/julia

就是说 2n 个 \ + 一个 " 就意味着字符串的结束,2n+1 个 \ + 一个 " 才能正确的表示转义后的英文双引号。
所以 r"[\\"]" 字符串提前结束了。

当然三引号的字符串转单引号的转换应该还是有问题的。

定这个转义规则的讨论

1 个赞

看上去 RE 使用了 pcre 的库,那就应该是多行字符串转常规字符串的问题。

看上去已经有人提过输出有问题了(2L)

输出确实是令人 confusing,估计是不影响使用就没修。

1 个赞