From: gnuist006@gmail.com
To: help-gnu-emacs@gnu.org
Subject: Re: replace-regexp-in-region?
Date: Tue, 25 Nov 2014 19:12:04 -0800 (PST) [thread overview]
Message-ID: <b0679574-a9e8-4f23-90e5-27934a55ecc8@googlegroups.com> (raw)
In-Reply-To: <vafu2t53wd1.fsf@petty.cs.uni-dortmund.de>
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]
parent reply other threads:[~2014-11-26 3:12 UTC|newest]
Thread overview: expand[flat|nested] mbox.gz Atom feed
[parent not found: <vafu2t53wd1.fsf@petty.cs.uni-dortmund.de>]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=b0679574-a9e8-4f23-90e5-27934a55ecc8@googlegroups.com \
--to=gnuist006@gmail.com \
--cc=help-gnu-emacs@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.