From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: "Bingham, Jay" Newsgroups: gmane.emacs.help Subject: RE: Newbie regexp question Date: Thu, 31 Oct 2002 12:11:20 -0600 Sender: help-gnu-emacs-admin@gnu.org Message-ID: <72A87F7160C0994D8C5A36E2FDC227F503926200@txnexc01.americas.cpqcorp.net> NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="----_=_NextPart_001_01C28108.EA09D185" X-Trace: main.gmane.org 1036090118 26963 80.91.224.249 (31 Oct 2002 18:48:38 GMT) X-Complaints-To: usenet@main.gmane.org NNTP-Posting-Date: Thu, 31 Oct 2002 18:48:38 +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 187KMt-00070c-00 for ; Thu, 31 Oct 2002 19:48:36 +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 187KJB-0001vB-00; Thu, 31 Oct 2002 13:44:45 -0500 Original-Received: from list by monty-python.gnu.org with tmda-scanned (Exim 4.10) id 187K9u-0008Cb-00 for help-gnu-emacs@gnu.org; Thu, 31 Oct 2002 13:35:10 -0500 Original-Received: from mail by monty-python.gnu.org with spam-scanned (Exim 4.10) id 187K6w-0007U6-00 for help-gnu-emacs@gnu.org; Thu, 31 Oct 2002 13:32:37 -0500 Original-Received: from zcamail03.zca.compaq.com ([161.114.32.103]) by monty-python.gnu.org with esmtp (Exim 4.10) id 187Jn7-0002Pt-00 for help-gnu-emacs@gnu.org; Thu, 31 Oct 2002 13:11:37 -0500 Original-Received: from cacexg11.americas.cpqcorp.net (cacexg11.americas.cpqcorp.net [16.105.250.94]) by zcamail03.zca.compaq.com (Postfix) with ESMTP id 96562537A for ; Thu, 31 Oct 2002 10:11:26 -0800 (PST) Original-Received: from txnexc01.americas.cpqcorp.net ([16.74.7.244]) by cacexg11.americas.cpqcorp.net with Microsoft SMTPSVC(5.0.2195.2966); Thu, 31 Oct 2002 10:11:21 -0800 X-MimeOLE: Produced By Microsoft Exchange V6.0.6249.0 content-class: urn:content-classes:message X-MS-Has-Attach: yes X-MS-TNEF-Correlator: Thread-Topic: Newbie regexp question Thread-Index: AcKA7G4en/oM0VlkRJSGIMvj6jfK9AAFE1+g Original-To: X-OriginalArrivalTime: 31 Oct 2002 18:11:21.0526 (UTC) FILETIME=[EA96AD60:01C28108] 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:3178 X-Report-Spam: http://spam.gmane.org/gmane.emacs.help:3178 This is a multi-part message in MIME format. ------_=_NextPart_001_01C28108.EA09D185 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Friedrich, Thanks for answering Paul's questions about the replace-between-regexp = function. I have one comment regarding the what appears to be a suggestion to = change the insert logic to (when repl-string (insert repl-string)). I do not believe that it is necessary to enclose the insert construct in = the when construct. Without the when construct if repl-string is empty = nothing gets inserted into the buffer, so it serves no purpose, the nil = that magically appeared is totally a byproduct of using the C-j to = invoke the function. Regarding Paul's question about where to put the function I agree that = it is best to put functions into a separate file and load or auto-load = them into emacs in the .emacs file. Here is the function in a file with instructions on how to load or = auto-load it. <>=20 -_ J_) C_)ingham . HP - NonStop Austin Software & Services - Software Quality = Assurance . Austin, TX . Language is the apparel in which your thoughts parade in public. . Never clothe them in vulgar and shoddy attire. -Dr. George W. = Crane- -----Original Message----- From: Friedrich Dominicus [mailto:frido@q-software-solutions.com]=20 Sent: Thursday, October 31, 2002 8:41 AM To: help-gnu-emacs@gnu.org Subject: Re: Newbie regexp question Paul Cohen writes: > Hi all, >=20 > Thanks to everyone who has been kind to take time to answer my = question! Jay's answer is definitely closest to solving my problem. >=20 > "Bingham, Jay" wrote: >=20 > > 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. >=20 > Neat. >=20 > > 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. >=20 > Ok. >=20 > > Here is my function if you want it. >=20 > Yes I do! :-) >=20 > > > > (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))))) >=20 > I have few comments/questions. >=20 > I tried the above function in my *scratch* buffer by writing it and = then adding the following lines (with line numbers!): >=20 > 19. (replace-between-regexp "" "" "" 1) > 20. > 21. Pub > 22. > 23. Foo > 24. > 25. Bar >=20 > I then evaluated the function with C-j. This resulted in: >=20 > 19. (replace-between-regexp "" "" "" 1) > 20. > 21. > 22. Pub > 23. nil > 24. > 25. Bar >=20 > With the cursor on line 24. My comments/questions are: >=20 > 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!=20 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.... "