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 19:22:35 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.50 (gnu/linux)

Ted Zlatanov <address@hidden> writes:

> SM> We could implement this feature efficiently by generalizing the `subexp'
> SM> argument so that rather than subgroup-number it can also take a special
> SM> new value `whole-string' which means "not just the whole matched text,
> SM> but the whole freakin' string".  Then you could do:
>
> SM>    (let ((regex "\\(alpha\\)")
> SM>          (string "gamma alpha beta"))
> SM>      (when (string-match regex string)
> SM>        (replace-match "found greek letter \\1" nil nil string 
> 'whole-string)))
>
> That would be great.  Then we wouldn't have to play the string-match
> regex escaping game above.  But it complicates the code a bit to
> provide `string'.

Hardly.

> I think Lars's suggestion to always save `string' to the same global
> variable is sensible;

For one thing I guess it is too late to change the API.  For another,
that prevents strings from being garbage-collected as long as they are
present in some match-data.  While the same is true of buffers, a dead
buffer does not take significant space.

> then we could say
>
>  (replace-match "found greek letter \\1" nil nil 'whole-last-string)
>
> to mean "replace in the whole freakin' string you last matched on" :)
>
> In a few tests your example worked great.  Lars, WDYT?
>
> In general, it would be really nice if there was a
> match-string/string-match/replace-match API variation that worked like
> Perl's $1...$9 and Perl's named captures.  Those simply contain the
> matched substring.

What's wrong with match-substitute-replacement ?

-- 
David Kastrup




reply via email to

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