将多个 replace-regexp 组合到一个程序中
Combine multiple replace-regexp to a program
我正在使用多个正则表达式替换清理文件
<<.*>> -> ""
\([[:alpha:]]\)\* -> ;; patters as pragram* to program
\*\([[:alpha:]]\) -> ;; patters as *program to program
\*/\([[:alpha:]]\) ->
;;and so on
在每个文件上,我都必须多次调用 replace-regexp
。
如何结合这些正则表达式搜索?
在某种程度上,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)))
我正在使用多个正则表达式替换清理文件
<<.*>> -> ""
\([[:alpha:]]\)\* -> ;; patters as pragram* to program
\*\([[:alpha:]]\) -> ;; patters as *program to program
\*/\([[:alpha:]]\) ->
;;and so on
在每个文件上,我都必须多次调用 replace-regexp
。
如何结合这些正则表达式搜索?
在某种程度上,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)))