在朱莉娅的原始文字字符串 [英] Raw literal strings in Julia

查看:146
本文介绍了在朱莉娅的原始文字字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python中,可以写入 ra\\\
b
,以防止 \\\
被解释为转义序列的换行符。

In Python one can write r"a\nb" in order to prevent the \n from being interpreted as an escape sequence for newline.

在Julia有类似的东西吗?而如果像$ variable这样的字符串插入怎么办?有没有办法阻止它?

Is there something similar in Julia? And what about string interpolation like "$variable", is there a way to prevent it?

我知道一个可以在Julia中写下a\\
b
\ $ variable喜欢写很多LaTeX字符串,而不必关心每个反斜杠 \ 和美元 $ 字符的正确转义。

I know one can simply write "a\\nb" and "\$variable" in Julia, but I would like to write a lot of LaTeX strings without having to care to proper escape every backslash \ and dollar $ characters...

(在Julia, r...创建一个正则表达式。)

(In Julia, r"..." creates a regular expression.)

推荐答案

好的,我刚刚发现,由于可以轻松创建非标准字符串字面值,我将要求的是:

Ok, I just found out that since one can easily create non-standard string literals in Julia, a pass-through one will do what I was asking for:

macro R_str(s)
    s
end

>>> R"$a\n$b"
"\$a\\n\$b"
>>> print(R"$a\n$b")
$a\n$b

我还发现 PyPlot.jl 定义了一个 LaTeXString 类型,可以通过 L....没有转义反斜杠或美元符号»,另外有益于在IJulia提出的方程式。

I also discovered that PyPlot.jl defines a «LaTeXString type which can be constructed via L"...." without escaping backslashes or dollar signs», with the additional benefit of rendered equations in IJulia.

最后一个问题是:不值得拥有一个原始的在朱莉娅基地的字符串字面量?

A last question remains: is not worth it to have a raw string literal in Julia base?

这篇关于在朱莉娅的原始文字字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆