0

I am working on cleaning files with multiple regex-replacement

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

On every single file, I have to invoke replace-regexp various times.

How could combine these regex search?

1 Answer 1

1

To an extent, M-x whitespace-cleanup has similar requirements, that is, cleanup base on multiple conditions. It should be possible to use (emacs) Keyboard Macros, but I am not familiar with it. Once you have some knowledge in Emacs Lisp, you can solve the problem easily, for example, the following cleanups leading and trailing spaces, you can add your regexp and their replacement into 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)))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.