我正在使用 rx 函数来从 ELISP 中的 sexps 生成正则表达式,但无法弄清楚如何生成正则表达式 "\documentclass" 以在 org-export-latex- 中使用课程:
(rx "\\documentclass")
(rx "\\" "documentclass")
(rx (char "\\") "documentclass")
评估时分别给出以下输出:
"\\\\documentclass"
"\\\\documentclass"
"[\\]documentclass"
“\ documentclass”相当于“[\] documentclass”吗?---我认为是,但不确定。我可以使用 rx 生成前者吗?
编辑:虽然这个问题是有效的,但我意识到我的动机不是;因为 org-export-latex-classes 使用字符串而不是正则表达式。
Emacs 要求
\
在字符串的双引号 read 语法 中转义,因此,当 lisp reader 处理代码时,"\\"
计算结果为包含单个 \
的字符串 object性格;因此,正则表达式引擎在使用该字符串对象时看到的是单个反斜杠。
但是,正则表达式中的
\
also 具有转义功能,这意味着正则表达式中的序列 \\
与单个 \
匹配。
为了表示 Emacs 字符串(的读取语法)中的序列
\\
,每个反斜杠本身都必须通过在其前面添加反斜杠来进行转义。
因此
"\\\\"
计算为包含 \\
的字符串,可用作匹配单个 \
的正则表达式。
然而,在正则表达式字符替代序列中,反斜杠是字面意思(它们不转义后面的字符);因此,由字符串 [\]
表示的
"[\\]"
匹配单个反斜杠——该单字符集唯一可能的匹配。因此用作正则表达式,字符串
"\\\\"
和
"[\\]"
匹配相同的内容。作为正则表达式的字符串
"\\documentclass"
实际上与
"documentclass"
相同,根本没有反斜杠,因为它是在正则表达式中转义的
d
(这是有效的,但当然是不必要的)。elisp手册对此的解释如下:
`\' has two functions: it quotes the special characters (including
`\'), and it introduces additional special constructs.
Because `\' quotes special characters, `\$' is a regular
expression that matches only `$', and `\[' is a regular expression
that matches only `[', and so on.
Note that `\' also has special meaning in the read syntax of Lisp
strings (*note String Type::), and must be quoted with `\'. For
example, the regular expression that matches the `\' character is
`\\'. To write a Lisp string that contains the characters `\\',
Lisp syntax requires you to quote each `\' with another `\'.
Therefore, the read syntax for a regular expression matching `\'
is `"\\\\"'.
[...]
As a `\' is not special inside a character alternative, it can never
remove the special meaning of `-' or `]'. So you should not quote
these characters when they have no special meaning either. This would
not clarify anything, since backslashes can legitimately precede these
characters where they _have_ special meaning, as in `[^\]' (`"[^\\]"'
for Lisp string syntax), which matches any single character except a
backslash.
C-hig(elisp) Regexp Special