* Re: replace-regexp-in-region?
[not found] ` <vafu2t53wd1.fsf@petty.cs.uni-dortmund.de>
@ 2014-11-26 3:12 ` gnuist006
0 siblings, 0 replies; only message in thread
From: gnuist006 @ 2014-11-26 3:12 UTC (permalink / raw)
To: help-gnu-emacs
On Saturday, May 22, 1999 12:00:00 AM UTC-7, kai.gro...@cs.uni-dortmund.de wrote:
> sen_ml@eccosys.com writes:
>
> > is there a 'replace-regexp-in-region' command?
>
> Emacs offers M-x narrow-to-region RET (bound to C-x n n by default)
> which allows one to restrict all kinds of editing commands to the
> region. Use M-x widen RET (or C-x n w) when you're done.
>
> From Lisp, the customary way to restrict replacing to a region is to
> use a different mechanism altogether. There is a replace-regexp
> command, the documentation contains the following lines:
>
> ,----- Excerpt from C-h f replace-regexp RET
> | This function is usually the wrong thing to use in a Lisp program.
> | What you probably want is a loop like this:
> | (while (re-search-forward REGEXP nil t)
> | (replace-match TO-STRING nil nil))
> | which will run faster and will not set the mark or print anything.
> `-----
I would like to see the above modified to a working example such that in replace-match a tagged subexpression defined in the REGEXP in re-search-forward is used to limit the match on which replace-match operates.
The problem I am having is that in this part, 1 is not working.
(save-excursion (while
(search-forward-regexp "START\\(.*\\)END" )
(save-match-data (replace-match (replace-regexp-in-string "." "Z" (match-string 0)) 1 ))
))
Thanks
Gnuist
>
> As you can see, this uses the function re-search-forward. Invoking
> C-h f on that, you quickly see that it allows to bound the search by a
> buffer position. So, the way to do it from Lisp is to go to the
> beginning of the region where you want replacing, then use a while
> look similar to the above but supplying the end position as a bound to
> re-search-forward.
>
> kai
> --
> Abort this operation? [Abort] [Cancel]
^ permalink raw reply [flat|nested] only message in thread