【问题标题】:Combine multiple replace-regexp to a program将多个替换正则表达式组合到一个程序中
【发布时间】:2019-11-18 09:32:38
【问题描述】:

我正在使用多个正则表达式替换清理文件

<<.*>>                  -> ""
\([[:alpha:]]\)\*       -> \1 ;; patters as pragram* to program
\*\([[:alpha:]]\)       -> \1 ;; patters as *program to program
\*/\([[:alpha:]]\)      -> \1
;;and so on

在每个文件中,我必须多次调用replace-regexp。

如何结合这些正则表达式搜索?

【问题讨论】:

    标签: regex replace emacs


    【解决方案1】:

    在某种程度上,M-x whitespace-cleanup 也有类似的要求,即基于多个条件进行清理。应该可以使用(emacs) Keyboard Macros,但是我不熟悉。一旦您对 Emacs Lisp 有所了解,您就可以轻松解决问题,例如,以下清理前导和尾随空格,您可以将您的正则表达式及其替换添加到 my-cleanup-regexps:

    (defvar my-cleanup-regexps
      '(("^ +" "")
        (" +$" ""))
      "A list of (REGEXP TO-STRING).")
    
    (defun my-cleanup-replace-regexp (regexp to-string)
      "Replace REGEXP with TO-STRING in the whole buffer."
      (goto-char (point-min))
      (while (re-search-forward regexp nil t)
        (replace-match to-string)))
    
    (defun my-cleanup ()
      "Cleanup the whole buffer according to `my-cleanup-regexps'."
      (interactive)
      (dolist (r my-cleanup-regexps)
        (apply #'my-cleanup-replace-regexp r)))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-21
      • 2012-02-16
      • 1970-01-01
      • 2020-01-29
      • 2012-03-01
      • 1970-01-01
      相关资源
      最近更新 更多