1

私はElispを初めて使用するので、LaTeXコードの一部をXMLに変換する必要があります。

ラテックス:

\tag[read=true]{Please help}\tag[notread=false]{Please help II}

XML:

<tag read='true'> Please help </tag>
<tag notread='false'> please help </tag>

検索して見つけるためにいくつかの正規表現を作成しまし\tagたが、今度はそれらを何らかの方法で読み取って属性として割り当て、「=」の後に値を読み取る必要がありreadますnotread。私が試した正規表現:

[..] (while (re-search-forward "\\\\\\<tag\\>\\[" nil t) [..]
4

1 に答える 1

1

これは完全な解決策ではありませんが、正規表現で後方参照を使用する方法を示していることを願っています。

簡単に言えば、正規表現で作成したすべてのグループ\\(...\\)がキャプチャされ、 で呼び出すことができます(match-string N)。ここNで、 はグループの連番で、左端の開き括弧の 1 から始まり、各開き括弧がより 1 大きい番号になるように進みます以前。

(したがって、代替がある場合、一部の後方参照は未定義になります。正規表現"\\(foo\\)\\|\\(bar\\)"を文字列"bar"に適用すると、(match-string 1)は空になり、 に(match-string 2)なります"bar"。)

(while
    (re-search-forward
     "\\\\\\<\\(tag\\)\\>\\[\\([^][=]*\\)=\\([^][]*\\)\\]{\\([^}]*\\)}"
     nil t)
  (insert (concat "<" (match-string 1) " "
          (match-string 2) "='" (match-string 3) "'>"
          (match-string 4)
          "</" (match-string 1) ">\n") ) )

その正規表現は確かに怪物です。ある程度分解して文書化したい場合があります。

(defconst latex-to-xml-regex
  (concat "\\\\"                ; literal backslash
          "\\<"                 ; word boundary (not really necessary)
          "\\(tag\\)"           ; group 1: capture tag
          "\\["                 ; literal open square bracket
          "\\("                 ; group 2: attribute name
            "[^][=]*"             ; attribute name regex
          "\\)"                 ; group 2 end
          "="                   ; literal
          "\\("                 ; group 3: attribute value
            "[^][]*"              ; attribute value regex
          "\\)"                 ; group 3 end
          "\\]"                 ; literal close square bracket
          "{"                   ; begin text group
          "\\("                 ; group 4: text
            "[^}]*"               ; text regex
          "\\)"                 ; group 4 end
          "}"                   ; end text group
          ) "Regex for `latex-to-xml` (assuming your function is called that)")
于 2012-09-04T06:25:01.613 に答える