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

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

Re: multiple search strings in query-replace-regexp


From: David Kastrup
Subject: Re: multiple search strings in query-replace-regexp
Date: Sat, 07 Oct 2006 10:40:39 +0200
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.0.50 (gnu/linux)

Malte Spiess <i1tnews@arcor.de> writes:

> David Kastrup <dak@gnu.org> writes:
>
>> "Florian Kaufmann" <sensorflo@gmail.com> writes:
>>
>>> that works only for multiple searches at once, but not for multiple
>>> search-replace at once.
>>>
>>> replace all foo by bar
>>> replace all rock by house
>>> replace all metal by goa
>>>
>>> :-) and so on
>>
>> With Emacs 22:
>>
>> C-M-% \(foo\)\|\(rock\)\|metal RET
>> \,(cond (\1 "bar") (\2 "house") ("goa")) RET
>
> Doesn't work with Emacs 21.4.1 :-(...

Emacs 22 is about to enter pretest (I'd guess within a week), so there
is little reason not to join the testers if one would like the
features.

> Looks pretty impressive though!
> Does \, cause to execute lisp stuff?

Yes.  The doc string spells this out, but the manual is a bit more
verbose:

   The remainder of this subsection is intended for specialized tasks
and requires knowledge of Lisp.  Most readers can skip it.

   You can use Lisp expressions to calculate parts of the replacement
string.  To do this, write `\,' followed by the expression in the
replacement string.  Each replacement calculates the value of the
expression and converts it to text without quoting (if it's a string,
this means using the string's contents), and uses it in the replacement
string in place of the expression itself.  If the expression is a
symbol, one space in the replacement string after the symbol name goes
with the symbol name, so the value replaces them both.

   Inside such an expression, you can use some special sequences.  `\&'
and `\N' refer here, as usual, to the entire match as a string, and to
a submatch as a string.  N may be multiple digits, and the value of
`\N' is `nil' if subexpression N did not match.  You can also use `\#&'
and `\#N' to refer to those matches as numbers (this is valid when the
match or submatch has the form of a numeral).  `\#' here too stands for
the number of already-completed replacements.

   Repeating our example to exchange `x' and `y', we can thus do it
also this way:

     M-x replace-regexp <RET> \(x\)\|y <RET>
     \,(if \1 "y" "x") <RET>

   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>


> Btw: Shouldn't
>
> C-M-% \(foo\|rock\)\|metal RET
> \,(cond (\1 "bar") (\1 "house") ("goa")) RET
>
> work, too?

No.  It would replace either "foo" or "rock" with "bar", and "metal"
with "goa".

-- 
David Kastrup, Kriemhildstr. 15, 44793 Bochum


reply via email to

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