转换" $$ ... $$"进入" \ [... \]"在emacs中使用replace-regexp

时间:2015-08-26 18:35:56

标签: regex emacs

我尝试使用emacs来转换表单的字符串" $$ ... $$"进入" \ [... \]"。我刚开始学习如何使用emacs来编辑某些文件。

在emacs wiki上关注replace-regexp的first example之后,我尝试了一下:

 M-x replace-regexp
 Replace regexp: \$\$.*\$\$
 Replace regexp with: \,("\[" \1 "\]")

但我收到错误:invalid function "["。我改为尝试了

Replace regexp with: "\["\1"\]"

但我收到错误:Invalid use of `\' in replacement text

由于这两种方法都不起作用,我尝试在emacs wiki上修改了13th example的replace-regexp,写作

M-x replace-regexp
Replace regexp: \$\$
Replace regexp with: \,(if (evenp \#) "\[" "\]")

但我收到错误:(void-function evenp)。有什么建议?我想了解每个实例中出现的问题,以及如何修复它们。

1 个答案:

答案 0 :(得分:3)

您不需要宏。

(replace-regexp "\\$\\$\\(.*?\\)\\$\\$"
                "\\\\[\\1\\\\]")

或者

Replace regexp: \$\$\(.*?\)\$\$
Replace with: \\[\1\\]

更新

  1. 使用贪婪的语法.*?来处理一行中出现多次的情况(来自@ phil'以下评论)。

  2. 请注意,当$$之间的文字跨越多行时,这不起作用。如果文本分布在多行中,则以下内容应该有效,不过在这一点上我可能更喜欢滚动自己的函数:

    (replace-regexp "\\$\\$\\(\\(.\\|\n\\)*?\\)\\$\\$"
                    "\\\\[\\1\\\\]")
    

    或者

    替换正则表达式:\$\$\(\(.\| C-q C-j \)*?\)\$\$
    替换为:\\[\1\\]

相关问题