From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: gnuist006@gmail.com Newsgroups: gmane.emacs.help Subject: Re: replace-regexp-in-region? Date: Tue, 25 Nov 2014 19:12:04 -0800 (PST) Message-ID: References: <19990522105846S.sen_ml@eccosys.com> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 X-Trace: ger.gmane.org 1416991055 3964 80.91.229.3 (26 Nov 2014 08:37:35 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 26 Nov 2014 08:37:35 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Wed Nov 26 09:37:32 2014 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1XtY6N-0007gF-R0 for geh-help-gnu-emacs@m.gmane.org; Wed, 26 Nov 2014 09:37:31 +0100 Original-Received: from localhost ([::1]:60864 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XtY6N-0001Sz-Ef for geh-help-gnu-emacs@m.gmane.org; Wed, 26 Nov 2014 03:37:31 -0500 X-Received: by 10.66.222.135 with SMTP id qm7mr29544327pac.20.1416971525119; Tue, 25 Nov 2014 19:12:05 -0800 (PST) X-Received: by 10.182.79.104 with SMTP id i8mr68obx.21.1416971524866; Tue, 25 Nov 2014 19:12:04 -0800 (PST) Original-Path: usenet.stanford.edu!h15no7768952igd.0!news-out.google.com!d20ni285igz.0!nntp.google.com!h15no477695igd.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Original-Newsgroups: gnu.emacs.help In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=99.103.155.107; posting-account=REkl4woAAABFXaU7nL79XtGpnmNCQ415 Original-NNTP-Posting-Host: 99.103.155.107 User-Agent: G2/1.0 Injection-Date: Wed, 26 Nov 2014 03:12:04 +0000 Original-Xref: usenet.stanford.edu gnu.emacs.help:208926 X-Mailman-Approved-At: Wed, 26 Nov 2014 03:37:15 -0500 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:101211 Archived-At: 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]