help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: replace-regexp-in-region?


From: gnuist006
Subject: Re: replace-regexp-in-region?
Date: Tue, 25 Nov 2014 19:12:04 -0800 (PST)
User-agent: G2/1.0

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]


reply via email to

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