简体   繁体   English

如何在emacs中用parantheses搜索/替换表达式?

[英]How to search/replace expressions with parantheses in emacs?

I have a bit of Latex code with lots of maths expressions enclosed in \\mathrm{}. 我有一些Latex代码,其中包含许多数学表达式,包含在\\ mathrm {}中。 I'd like to remove the \\mathrm{} code around the expressions, preferrably using emacs. 我想删除表达式周围的\\ mathrm {}代码,最好使用emacs。 For example, I'd like to substitute 例如,我想替换

\mathrm{\gamma \cdot x_0}

with

\gamma \cdot x_0

Removing \\mathrm{ only is easy, but I also need to remove the closing bracket. 删除\\ mathrm {只是很容易,但我还需要删除结束括号。 How can I do this in emacs? 我怎么能在emacs中这样做?

Many thanks, 非常感谢,

Enno 恩诺

You can use back references to tackle this problem. 您可以使用反向引用来解决此问题。 Run

Mx query-replace-regexp Mx query-replace-regexp

and enter \\\\mathrm{\\([\\a-z0-9_ ]+\\)} at the first prompt, \\1 at the second prompt. 并在第一个提示符下输入\\\\mathrm{\\([\\a-z0-9_ ]+\\)} ,在第二个提示符下输入\\1

The default keybinding for query-replace-regexp is CM-% . query-replace-regexp的默认键绑定是CM-%

The \\1 is a back reference to the first parenthesized group, \\([\\a-z0-9_ ]+\\) , in the regexp to replace. \\1是要替换的正则表达式中第一个带括号的组, \\([\\a-z0-9_ ]+\\)的后引用。 This group targets the content between the curly brackets. 该组以大括号之间的内容为目标。 So what you are saying is that for any occurrence of the regexp to replace you would only like to keep that content. 所以你要说的是,对于任何要替换的正则表达式,你只想保留那些内容。


More info on replacing regular expressions can be found here or in the corresponding info node of the Emacs manual. 有关替换正则表达式的更多信息可以在此处或在Emacs手册的相应info节点中找到。

The command query-replace-regexp is quite flexible. 命令query-replace-regexp非常灵活。 You can set replace-re-search-function to your own search function. 您可以将replace-re-search-function为您自己的搜索功能。 On this basis the following lisp code defines a new command query-replace-re+sexp which searches for a regular expression but includes the trailing sexp into the match. 在此基础上,以下lisp代码定义了一个新命令query-replace-re+sexp ,它搜索正则表达式,但在匹配中包含尾随的sexp。

After evaluating the defun below you can use Mx query-replace-re+sexp as query-replace function. 评估后defun下面你可以使用Mx query-replace-re+sexp作为查询替换功能。 In your example, input as "from"-string \\\\\\\\mathrm and as "replace-with"-string \\\\1 . 在您的示例中,输入为“from”-string \\\\\\\\mathrm和“replace-with”-string \\\\1 There, the expression \\\\1 refers to the additional subexpression which results from the trailing sexp (without delimiters). 在那里,表达式\\\\1指的是由尾随的sexp(没有分隔符)产生的附加子表达式。

(defun re+sexp-search-forward (regexp bound noerror)
  "Search forward for REGEXP (like `re-search-forward')
but with appended sexp."
  (when (re-search-forward regexp bound noerror)
    (let ((md (match-data))
      bsub esub)
      (setq bsub (1+ (scan-sexps (goto-char (scan-sexps (point) 1)) -1))
        esub (1- (point)))
      (setcar (cdr md) (set-marker (make-marker) (point)))
      (setq md (append md (list (set-marker (make-marker) bsub)
                (set-marker (make-marker) esub))))
      (set-match-data md)
      (point))))

(defun query-replace-re+sexp ()
  "Like `query-replace-regexp' but at each match it includes the trailing sexps
into the match as an additional subexpression (the last one)."
  (interactive)
  (let ((replace-re-search-function 're+sexp-search-forward)) (call-interactively 'query-replace-regexp)))

This should be a quite nice feature. 这应该是一个非常好的功能。 Not only for the replacement of LaTeX constructs like 不仅仅是替代像LaTeX这样的结构

\mathrm{\frac{\gamma}{x_0}}

with

\frac{\gamma}{x_0}

but also for replacements in program texts. 但也用于程序文本的替换。 For an example the replacement of function calls like 举个例子,替换函数调用就像

doSomethingUseless(x+somethingUseful(y))

with

x+somethingUseful(y)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM