GREP - Matching everything between two delimiters

932 views
Skip to first unread message

dp

unread,
Oct 20, 2011, 2:17:23 PM10/20/11
to BBEdit Talk
I need to find and replace everything between and including the
delimiters
<!-- Begin value list -->
and
<!-- End value list -->
The text to be replaced includes returns. Early attempts with (.*)
found only the opening delimiter and the next line, stopped by the
return at the end of the second line. Attempts to get past the returns
using (?s).+ ignore the closing delimiter and swallow the remainder of
the document. Any help would be appreciated.

Ronald J Kimball

unread,
Oct 20, 2011, 2:33:32 PM10/20/11
to bbe...@googlegroups.com

Use .*? instead of .* for non-greedy matching.

Ronald

dp

unread,
Oct 20, 2011, 3:01:45 PM10/20/11
to BBEdit Talk


On Oct 20, 1:33 pm, Ronald J Kimball <r...@tamias.net> wrote:

> Use .*? instead of .* for non-greedy matching.

Thanks, Ronald. Unfortunately, all that results is a system beep.
Here's what I've got. Have I misunderstood you?
(<!-- Begin value list -->)(.*?)(<!-- End value list -->)

John Delacour

unread,
Oct 20, 2011, 3:15:24 PM10/20/11
to bbe...@googlegroups.com


If you are doing your substitutions line by line then a text filter
like this will do it:


#! /usr/bin/perl
use strict;
my $on;
while (<>) {
$on = 1 and print and next if /<!-- Begin value list -->/;
$on = 0 if /<!-- End value list -->/;
if ($on) { # actions when switched on
s/.+/*****$&*****/;
}
print;
}
__END__

If you need to cross over line endings then the solution is a little different.

JD

Ronald J Kimball

unread,
Oct 20, 2011, 3:22:08 PM10/20/11
to bbe...@googlegroups.com

You still need (?s) to allow . to match newlines.

Ronald

dp

unread,
Oct 20, 2011, 3:37:59 PM10/20/11
to BBEdit Talk
Both my needs and my knowledge are much simpler. The value list master
is kept in a single file. The value list is used in six places in
other files. The delimiters bracket each place where it is used. When
the value list changes, I change the master, copy the master, and
replace each instance in the other files. I can drop the master into
the Replace field of the Find/Replace window. I'm just trying to
develop a search string that will allow me to find the delimiters and
everything in between, so that I can replace all instances at once,
rather than individually.

dp

unread,
Oct 20, 2011, 3:58:13 PM10/20/11
to BBEdit Talk
I'm not following. Still nothing but a beep.

Ronald J Kimball

unread,
Oct 20, 2011, 4:12:13 PM10/20/11
to bbe...@googlegroups.com

Try this:

(?s)(<!-- Begin value list -->)(.*?)(<!-- End value list -->)

Ronald

dp

unread,
Oct 20, 2011, 4:18:31 PM10/20/11
to BBEdit Talk
That got it! Thank you, Ronald.
Reply all
Reply to author
Forward
0 new messages