[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 <per.nordlow@gmail.com> 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
- Advanced query-replace-regexp in code, Nordlöw, 2009/05/27
- Re: Advanced query-replace-regexp in code, Pascal J. Bourguignon, 2009/05/27
- Re: Advanced query-replace-regexp in code, Barry Margolin, 2009/05/27
- Re: Advanced query-replace-regexp in code, Teemu Likonen, 2009/05/28
- Re: Advanced query-replace-regexp in code, Barry Margolin, 2009/05/28
- Re: Advanced query-replace-regexp in code, Johan Bockgård, 2009/05/28
- Message not available
- Re: Advanced query-replace-regexp in code, Barry Margolin, 2009/05/28
- Re: Advanced query-replace-regexp in code, Nordlöw, 2009/05/30
- Re: Advanced query-replace-regexp in code, Johan Bockgård, 2009/05/30
- Message not available
- Re: Advanced query-replace-regexp in code, Barry Margolin, 2009/05/30
Re: Advanced query-replace-regexp in code,
harven <=