[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Newbie regexp question

From: Bingham, Jay
Subject: RE: Newbie regexp question
Date: Wed, 30 Oct 2002 15:12:31 -0600


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)))
             (if (re-search-forward end-re nil t)
                 (if incl (match-end 0) (match-beginning 0))
      (if (not end)
          (error "Unmatched \"%s\" sequence at position %d" start-re beg)
        (delete-region beg end)
        (insert repl-str)))))

.    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 [] 
Sent:   Wednesday, October 30, 2002 11:43 AM
Subject:        Re: Newbie regexp question

Michael Slass <> writes:

> I think a lisp program would do better at this:
> (defun paulc-purge-html-test-sections (buffer)
>   "Delete all occurances of text between <!--Test--> and <!--End of Test-->, 
> inclusive."
>   (interactive "bPurge html test sections in buffer: ")
>   (save-excursion
>     (save-restriction
>       (goto-char (point-min))
>       (while (re-search-forward "<!--Test-->" nil t)
>         (let ((beg (match-beginning 0))
>               (end (progn (re-search-forward "<!--End of Test-->" nil t)
>                           (match-end 0))))
>           (if end
>               (kill-region beg end)
>             (error "Unmatched \"<!--Test-->\" 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

However a really nice solution anyway I think there is a problem with
the end stuff. 

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. 

So good night
Help-gnu-emacs mailing list

reply via email to

[Prev in Thread] Current Thread [Next in Thread]