[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: replace-regexp question
From: |
David Kastrup |
Subject: |
Re: replace-regexp question |
Date: |
Wed, 26 Oct 2005 21:52:36 +0200 |
User-agent: |
Gnus/5.11 (Gnus v5.11) Emacs/22.0.50 (gnu/linux) |
Giorgos Keramidas <keramida@ceid.upatras.gr> writes:
> David Kastrup <dak@gnu.org> writes:
>>Neon Absentius <absent@sdf.lonestar.org> writes:
>>> Is there a way to use the command replace-regexp to replace a number with
>>> that number increased, say, by 15? I tried
>>>
>>> C-M-% -?[0-9]+ RET (number-to-string (+ (string-to-number "\&") 15)) RET
>>>
>>> but it doesn't work because emacs interprets the second input to be
>>> a string and so it doesn't evaluate it. So I guess the question is
>>> how can we use the result of evaluating a sexp for replacement text?
>>>
>>> Any help and/or pointers to documentation will be greatly
>>> appreciated.
>>
>> The development version of Emacs would offer
>> C-M-% -?[0-9]+ RET \,(+ \#& 15) RET
>
> Oh, heh! Very neat ;)
The example in the Emacs manual is more amusing in my opinion, but
then I am biased.
For computing replacement strings for `\,', the `format' function is
often useful (*note Formatting Strings: (elisp)Formatting Strings.).
For example, to add consecutively numbered strings like `ABC00042' to
columns 73 to 80 (unless they are already occupied), you can use
M-x replace-regexp <RET> ^.\{0,72\}$ <RET>
\,(format "%-72sABC%05d" \& \#) <RET>
--
David Kastrup, Kriemhildstr. 15, 44793 Bochum