From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: Friedrich Dominicus Newsgroups: gmane.emacs.help Subject: Re: Newbie regexp question Date: 31 Oct 2002 15:41:22 +0100 Organization: Q Software Solutions GmbH Sender: help-gnu-emacs-admin@gnu.org Message-ID: <87pttqwu0d.fsf@fbigm.here> References: <3DC13711.6F82B396@enea.se> NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: main.gmane.org 1036075589 27981 80.91.224.249 (31 Oct 2002 14:46:29 GMT) X-Complaints-To: usenet@main.gmane.org NNTP-Posting-Date: Thu, 31 Oct 2002 14:46:29 +0000 (UTC) Return-path: Original-Received: from monty-python.gnu.org ([199.232.76.173]) by main.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 187GaY-0007H2-00 for ; Thu, 31 Oct 2002 15:46:26 +0100 Original-Received: from localhost ([127.0.0.1] helo=monty-python.gnu.org) by monty-python.gnu.org with esmtp (Exim 4.10) id 187Gab-0007tz-00; Thu, 31 Oct 2002 09:46:29 -0500 Original-Path: shelby.stanford.edu!newsfeed.stanford.edu!skynet.be!skynet.be!newsfeed.online.be!newsfeed01.sul.t-online.de!newsmm00.sul.t-online.com!t-online.de!news.t-online.com!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 146 Original-X-Trace: news.t-online.com 1036075242 00 7089 eHW9bxuSSXANHt 021031 14:40:42 Original-X-Complaints-To: abuse@t-online.com X-Sender: 320004655587-0001@t-dialin.net User-Agent: Gnus/5.0808 (Gnus v5.8.8) XEmacs/21.4 (Common Lisp) Original-Xref: shelby.stanford.edu gnu.emacs.help:106619 Original-To: help-gnu-emacs@gnu.org Errors-To: help-gnu-emacs-admin@gnu.org X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.0.11 Precedence: bulk List-Help: List-Post: List-Subscribe: , List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: Xref: main.gmane.org gmane.emacs.help:3170 X-Report-Spam: http://spam.gmane.org/gmane.emacs.help:3170 Paul Cohen writes: > Hi all, > > Thanks to everyone who has been kind to take time to answer my question! Jay's answer is definitely closest to solving my problem. > > "Bingham, Jay" wrote: > > > After seeing Mike and Friedrich's exchange on your question I decided to create my own solution, a general purpose function that allows the user to specify the start and end as regular expressions and supply a replacement for the text between them, and optionally by giving a numeric prefix argument to the function force it to replace the tags as well. > > Neat. > > > In the process I noticed that Mike's function has a logic flaw. It will never find the case of a missing end tag and instead deletes that final start tag. > > Ok. > > > Here is my function if you want it. > > Yes I do! :-) > > > > > (defun replace-between-regexp (start-re end-re repl-str &optional incl) > > "Replace the text between two regular expressions supplied as arguments. > > With a numeric argument the regular expressions are included. > > When called non interactively incl should be nil for non-inclusion and > > non-nil for inclusion." > > (interactive "sStart regexp: \nsEnd regexp: \nsReplace between %s and %s with: \nP") > > (while (re-search-forward start-re nil t) > > (let ((beg (if incl (match-beginning 0) (match-end 0))) > > (end > > (progn > > (if (re-search-forward end-re nil t) > > (if incl (match-end 0) (match-beginning 0)) > > nil)))) > > (if (not end) > > (error "Unmatched \"%s\" sequence at position %d" start-re beg) > > (delete-region beg end) > > (insert repl-str))))) > > I have few comments/questions. > > I tried the above function in my *scratch* buffer by writing it and then adding the following lines (with line numbers!): > > 19. (replace-between-regexp "" "" "" 1) > 20. > 21. Pub > 22. > 23. Foo > 24. > 25. Bar > > I then evaluated the function with C-j. This resulted in: > > 19. (replace-between-regexp "" "" "" 1) > 20. > 21. > 22. Pub > 23. nil > 24. > 25. Bar > > With the cursor on line 24. My comments/questions are: > > 1) I understand that the "nil" on line 23 comes from the value of > the last item in the function list, in this case "(insert > repl-str)". But there is also a newline character is inserted after > "nil". But I don't want either the "nil" or the extra newline > character! I'm a bit lazy to answer all or suggest other things but here's are my thoughts on that Call it with (replace-betwe.... "