1

ELISPSEPから正規表現を生成するrx関数をいじっていましたが、org-export-latex-classes で使用する正規表現"\\documentclass"を生成する方法がわかりませんでした。

    (rx "\\documentclass")
    (rx "\\" "documentclass")
    (rx (char "\\") "documentclass")

評価すると、それぞれ次の出力が得られます。

    "\\\\documentclass"
    "\\\\documentclass"
    "[\\]documentclass"

"\\documentclass""[\\]documentclass "と同等ですか? --- そうだと思いますが、よくわかりません。rx を使用して前者を生成できますか?

編集:質問は有効でしたが、私の動機はそうではなかったことに気づきました。org-export-latex-classes は正規表現ではなく文字列を使用するためです。

4

1 に答える 1

4

Emacs は、文字列\の二重引用符で囲まれた読み取り構文でエスケープする必要があるため、コードがLisp リーダーによって処理されると、単一の文字を含む文字列オブジェクト"\\"評価されます。したがって、その単一のバックスラッシュは、その文字列オブジェクトを使用するときに正規表現エンジンが認識するものです。\

ただし、\正規表現の a にはエスケープ機能\\あります。これは、正規表現のシーケンスが単一の に一致することを意味し\ます。

Emacs 文字列 (の読み取り構文) でシーケンスを表すには\\、それらのバックスラッシュのそれぞれにバックスラッシュを前置してエスケープする必要があります。

したがって、単一の に一致する正規表現として使用できる which を"\\\\"含む文字列に評価されます。\\\

ただし、正規表現文字の代替シーケンス内では、バックスラッシュはエスケープされません。そのため[\]、文字列 で表される は"[\\]"、単一のバックスラッシュに一致します。これは、その単一文字セットで唯一可能な一致です。

正規表現として使用されるため、文字列"\\\\""[\\]"一致するものは同じです。

正規表現としての文字列は、バックスラッシュがまったくない"\\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 RET

于 2012-06-02T16:33:52.867 に答える