これは完全な解決策ではありませんが、正規表現で後方参照を使用する方法を示していることを願っています。
簡単に言えば、正規表現で作成したすべてのグループ\\(...\\)
がキャプチャされ、 で呼び出すことができます(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)")