[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 10:57:31 -0600


It is too bad that you do not understand what Paul wants to do.


Here is what I understand you want to do: 
find the next occurrence of <!--Test-->, find the next occurrence <!--End of 
Test-->, delete everything between the start of <!--Test--> and end of <!--End 
of Test-->, do this repeatedly in the buffer.

There are some problems with your approach to this.
First, you did not end the pattern <!--Test--> with an end of line (you should 
probably also end the pattern <!--End of Test--> with an end of line, unless 
you want a blank line where these were found).
Second, regexp does not recognize "\n" as a new line it just puts an "n" in the 
pattern.  In order to match new lines you have to put literal new line 
characters in the pattern.  This is done with the C-q C-j sequence.
Third, even if you make the above corrections and produce the following pattern:
^[ \t]*<!--Test-->
\)*[ \t]*<!--End of Test-->

it still will not do what you want it to do.  The reason that it won't is that 
the asterisk at the end of the sub-pattern \(.*
\)* tells emacs to match as many as possible of the preceding sub-pattern.  So 
this will match from the start of the first <!--Test--> to the end of the last 
<!--End of Test-->.  Not exactly what I think you had in mind.

The only way that I know to do what you want to do is to write a function to do 
it.  This function would prompt for the first pattern, then prompt for the 
second pattern, then prompt for a replacement string.  It would then search for 
the first pattern, save the start location of the first pattern, search for the 
second pattern and replace the range between the start of the first pattern and 
the end of the second pattern with the replacement string.  (I say replacement 
string rather than pattern because the \DIGIT meta which is the only pattern 
meta that is of use in a replacement pattern would not work without doing some 
special coding in the function to simulate its operation).
I used to have a function that would delete everything between two patterns, 
but when I left my last employer I failed to capture a copy of it.  I kick my 
self quite often for not capturing the functions that I developed there.

.    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 9:34 AM
Subject:        Re: Newbie regexp question

Paul Cohen <> writes:

> Hi
> I want to do a Emacs regexp search and replace on a HTML file containing
> patterns like this:
> <!--Test-->
> ...
> <!--End of Test-->
> Where "..." denotes a variable number of lines of HTML text.
> I want to search for all occurrences of the above pattern and then
> remove them from the HTML file!
> I've tried a number of variants without any success. For example the
> following regexp doesn't work:
> <!--Test-->\(.*\n\)*<!--End of Test-->
I would restate the problem. It does not make much sense to me to
match over a bunch of lines you do not want to handle. 

So how about
M-C-% ^[ \t]*<!--.*Test.*--> with: RET

Or even better if you kow exactly what you are looking for
using replace-string?

Help-gnu-emacs mailing list

reply via email to

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