help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Advanced query-replace-regexp in code


From: harven
Subject: Re: Advanced query-replace-regexp in code
Date: Thu, 28 May 2009 21:50:41 +0200
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.1 (darwin)

Nordlöw <address@hidden> writes:

> I can't get the following example to work programmatically.
>
>   M-x replace-regexp
>   Replace regexp:  \(\w+\)
>   Replace regexp with:  \,(capitalize \1)
>
> Is this only possible in interactive query-replace?
> If so should I use a combination of while(), looking-at(), re-search-
> forward(), replace-match(), match-string() etc.
>
> Thanks in advance,
> Per Nordlöw

(defun my-cap ()
  (interactive)
  (while (re-search-forward "\\w+" nil t)
     (replace-match (capitalize (match-string 0)) nil t)))


>From the doc string of replace-regexp:

[replace-regexp] is usually the wrong thing to use in a Lisp program.
What you probably want is a loop like this:
  (while (re-search-forward REGEXP nil t)
    (replace-match TO-STRING nil nil))
which will run faster and will not set the mark or print anything.

hope that helps


reply via email to

[Prev in Thread] Current Thread [Next in Thread]