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是组的序号,从最左边的左括号开始,然后继续进行,以使每个左括号的数字高于以前的。

(所以如果你有交替,一些反向引用将是未定义的。如果你将正则表达式"\\(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 回答