[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 |
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]
Sent: Wednesday, October 30, 2002 11:43 AM
To: help-gnu-emacs@gnu.org
Subject: Re: Newbie regexp question
Michael Slass <miknrene@drizzle.com> writes:
>
> I think a lisp program would do better at this:
>
> VERY LIGHTLY TESTED. MAKE BACKUPS BEFORE EXPERIMENTING WITH 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
data.
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
Friedrich
_______________________________________________
Help-gnu-emacs mailing list
Help-gnu-emacs@gnu.org
http://mail.gnu.org/mailman/listinfo/help-gnu-emacs
- Re: Newbie regexp question, (continued)
- Re: Newbie regexp question, Friedrich Dominicus, 2002/10/30
- Re: Newbie regexp question, Michael Slass, 2002/10/30
- Re: Newbie regexp question, Friedrich Dominicus, 2002/10/30
- Re: Newbie regexp question, Michael Slass, 2002/10/30
- Re: Newbie regexp question, Michael Slass, 2002/10/30
Re: Newbie regexp question, Barry Margolin, 2002/10/30
Re: Newbie regexp question, Stefan Monnier <address@hidden>, 2002/10/30
Re: Newbie regexp question, kgold, 2002/10/31
RE: Newbie regexp question, Bingham, Jay, 2002/10/30
RE: Newbie regexp question,
Bingham, Jay <=
Re: Newbie regexp question, Paul Cohen, 2002/10/31
RE: Newbie regexp question, Bingham, Jay, 2002/10/31