[Top][All Lists]

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

Re: replace matches in any string

From: Ted Zlatanov
Subject: Re: replace matches in any string
Date: Thu, 02 Sep 2010 08:10:38 -0500
User-agent: Gnus/5.110011 (No Gnus v0.11) Emacs/24.0.50 (gnu/linux)

On Thu, 02 Sep 2010 13:29:41 +0200 Stefan Monnier <address@hidden> wrote: 

SM> I do not understand the feature you request.
SM> Rather than code that supposedly implements the feature, try to post
SM> examples of uses you'd like to see work.

I believe the following is accurate.  Daniel, please correct me if I'm
wrong in my description.


(let ((regex "\\(alpha\\)")
      (string "gamma alpha beta"))
  (when (string-match regex string)
    (replace-match "[first greek letter \\1]" nil nil string 1)))
-> "gamma [first greek letter alpha] beta"

We want the equivalent but against any string, not just the original
string (the `replace-match' docs say STRING has to be the one used for
the original `string-match').  The use case is when you say to match X
in an e-mail address, then set the target group to "something-X-other".
We want to do it with regex "\\(X\\)" against "address@hidden" and
target group "something-\\1-other".  Does that make sense?  Basically we
want \1 but without the context of that original string we matched:

(let ((regex "\\(alpha\\)")
      (string "gamma alpha beta"))
  (when (string-match regex string)
    (our-new-function "found greek letter \\1")))
-> "found greek letter alpha"

As I said, `nnmail-expand-newtext' implements this for Gnus purposes and
it can handle \[0-9] arguments and \& for the whole match but it's not
generic.  It needed to be extended for Daniel Dehennin's patch and so we
came to this.

SM> I have the impression that what you want is already provided by
SM> replace-match, tho only in the case when the match was performed on
SM> a string rather than in a buffer.

I hope so.


reply via email to

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