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: Wed, 30 Oct 2002 15:12:31 -0600 Sender: help-gnu-emacs-admin@gnu.org Message-ID: <72A87F7160C0994D8C5A36E2FDC227F50392602A@txnexc01.americas.cpqcorp.net> NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable X-Trace: main.gmane.org 1036012938 19002 80.91.224.249 (30 Oct 2002 21:22:18 GMT) X-Complaints-To: usenet@main.gmane.org NNTP-Posting-Date: Wed, 30 Oct 2002 21:22:18 +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 1870I4-0004wE-00 for ; Wed, 30 Oct 2002 22:22:16 +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 1870A6-0005gt-00; Wed, 30 Oct 2002 16:14:02 -0500 Original-Received: from list by monty-python.gnu.org with tmda-scanned (Exim 4.10) id 18708n-0003ZB-00 for help-gnu-emacs@gnu.org; Wed, 30 Oct 2002 16:12:41 -0500 Original-Received: from mail by monty-python.gnu.org with spam-scanned (Exim 4.10) id 18708l-0003Wf-00 for help-gnu-emacs@gnu.org; Wed, 30 Oct 2002 16:12:40 -0500 Original-Received: from zcamail04.zca.compaq.com ([161.114.32.104]) by monty-python.gnu.org with esmtp (Exim 4.10) id 18708k-0003W2-00 for help-gnu-emacs@gnu.org; Wed, 30 Oct 2002 16:12:38 -0500 Original-Received: from cacexg11.americas.cpqcorp.net (cacexg11.americas.cpqcorp.net [16.105.250.94]) by zcamail04.zca.compaq.com (Postfix) with ESMTP id DCF0EF64; Wed, 30 Oct 2002 13:12:36 -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); Wed, 30 Oct 2002 13:12:32 -0800 X-MimeOLE: Produced By Microsoft Exchange V6.0.6249.0 content-class: urn:content-classes:message X-MS-Has-Attach: X-MS-TNEF-Correlator: Thread-Topic: Newbie regexp question Thread-Index: AcKAQzcJ4E7QKZX5SL+63mvRiXXvKAAFKd2w Original-To: "Friedrich Dominicus" , X-OriginalArrivalTime: 30 Oct 2002 21:12:32.0899 (UTC) FILETIME=[1004CD30:01C28059] 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:3138 X-Report-Spam: http://spam.gmane.org/gmane.emacs.help:3138 Paul, 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. 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. Here is my function if you want it. (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))))) -_ 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: Wednesday, October 30, 2002 11:43 AM To: help-gnu-emacs@gnu.org Subject: Re: Newbie regexp question Michael Slass writes: >=20 > I think a lisp program would do better at this: >=20 > VERY LIGHTLY TESTED. MAKE BACKUPS BEFORE EXPERIMENTING WITH THIS! >=20 > (defun paulc-purge-html-test-sections (buffer) > "Delete all occurances of text between and , inclusive." > (interactive "bPurge html test sections in buffer: ") > (save-excursion > (save-restriction > (goto-char (point-min)) > (while (re-search-forward "" nil t) > (let ((beg (match-beginning 0)) > (end (progn (re-search-forward "" nil = t) > (match-end 0)))) > (if end > (kill-region beg end) > (error "Unmatched \"\" sequence at position %d" beg))))))) Well this code is better in some areas, but Mike you missed a big opportunity ;-) To let the user choose what the tags are and as mentioned before regular expressions are overkill if you know your data. However a really nice solution anyway I think there is a problem with the end stuff.=20 The info pages say: Search forward from point for regular expression REGEXP. Set point to the end of the occurrence found, and return point. That means you will return the End tags too, if I got that right, which is not sure I'm a tired and had an unpleasant quarrel with someone I really appriciate.=20 So good night Friedrich _______________________________________________ Help-gnu-emacs mailing list Help-gnu-emacs@gnu.org http://mail.gnu.org/mailman/listinfo/help-gnu-emacs