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: Lars Magne Ingebrigtsen
Subject: Re: replace matches in any string
Date: Thu, 02 Sep 2010 16:44:09 +0200
User-agent: Gnus/5.110011 (No Gnus v0.11) Emacs/24.0.50 (gnu/linux)

Ted Zlatanov <address@hidden> writes:

> 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"

If you look at the low-level stuff that's actually saved by
`string-match' and friends, it's basically a list of start/end points,
and that's it, I think.

(progn (string-match "\\(alpha\\)" "gamma alpha beta")
       (match-data))
=> (6 11 6 11)

So when you say `(match-string 1)', it just uses the range 6-11 and does
a buffer-substring on that, or, if you give the latter function a string
parameter, then a substring on that.

So, for reasons of efficiency (I'm guessing), the actual substrings that
were matched aren't stored anywhere.

Now, the simplest change here would be if `string-match' and friends
also saved what it did the match on in addition to the current match
data.  Then things like

(replace-match "letter \\1" nil nil t)

would "remember" that we're referring to the string in question.  The
performance impact should be minimal -- just an extra `setq' on a global
C variable that would refer to the string we matched.

-- 
(domestic pets only, the antidote for overdose, milk.)
  address@hidden * Lars Magne Ingebrigtsen




reply via email to

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