这不是一个完整的解决方案,但希望演示如何将反向引用与正则表达式一起使用。
简而言之,您在正则表达式中创建的每个组都会\\(...\\)
被捕获,并且可以用 调用(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)")