emacs-devel
[Top][All Lists]
Advanced

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

Re: replace matches in any string


From: David Kastrup
Subject: Re: replace matches in any string
Date: Thu, 02 Sep 2010 18:45:32 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.50 (gnu/linux)

Stefan Monnier <address@hidden> writes:

> I think I'm beginning to understand.
> First, you don't want
>
>   (replace-match "[first greek letter \\1]" nil nil string 1)
> but
>   (replace-match "[first greek letter \\1]" nil nil string)
>
> then second, you do get the behavior you want in the particular case
> where the string-match matched the whole string.
> So to simulate:
>
>> (let ((regex "\\(alpha\\)")
>>       (string "gamma alpha beta"))
>>   (when (string-match regex string)
>>     (our-new-function "found greek letter \\1")))
>> -> "found greek letter alpha"
>
> you'd want:
>
>    (let ((regex "\\(alpha\\)")
>          (string "gamma alpha beta"))
>      (when (string-match (concat "\\`.*?\\(?:" regex "\\).*\\'") string)
>        (replace-match "found greek letter \\1" nil nil string)))

You mean like

(let ((regex "\\(alpha\\)")
      (string "gamma alpha beta"))
   (when (string-match regex string)
     (match-substitute-replacement "found greek letter \\1" nil nil string)))

-- 
David Kastrup




reply via email to

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