On Mon, Sep 13, 2021 at 12:28:24PM +0300, Jean Louis wrote: > Dear Felix, maybe you forgot to insert mailing list in your answer. > > > Jean Louis writes: > > > Is there any regular expression counter replacement so that each new > > > replacement get a new number? > > > Example replacement: > > > \,(1+ \#) > > > This starts counting at 1. If you want to start at 0, then simply use: > > \# > > That is definitely interesting, please correct me, as I don't get it how to apply it, as following is not working: > > (replace-regexp-in-string "ABC" "\#" "ABCM ABCI ABCJ ABCY ABC8") I think this notation is only for interactive input. Besides, the wrapping "\,(...)" is important, meaning "interpolate this Lisp expression's value here". For what you have in mind you could make use of the fact that `replace-regexp-in-string' also takes a function: (defun make-counter (startval) ; the "classical" counter closure (lambda (match) (prog1 (format "%d" startval) ; we have to evaluate to a string, I think (setq startval (1+ startval))))) (replace-regexp-in-string "ABC" (make-counter 23) "ABCM ABCI ABCJ ABCY ABC8") => "23M 24I 25J 26Y 278" (don't forget lexical binding, it won't work otherwise ;-) Cheers - t