simple (so I thought) grep Q:

70 views
Skip to first unread message

Govinda

unread,
Sep 5, 2010, 4:22:08 PM9/5/10
to BBEdit Talk
Hi All

I am trying to modify a bunch of templates at once.
I need to do a grep search/replace:
find:
one string literal.. followed by any number of any chars...
followed by another string literal
replace:
(null) (I'll build the replace later, once I have the search
pattern right)

I thought it was simple.

My first string is:
"<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//
EN"><html><head>"

My second string is:
"[include file=/inc/ltnavbar.inc]
</td>"

(BBedit 9.5.1, grep on)
I tried a few patterns which did not find anything.. and then I tried
this:

search:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4\.01 Transitional//
EN"><html><head>(\s|\S)*?\[include file=/inc/ltnavbar\.inc]\r </td>

..and only get this:
error code 12246 (out of stack space)

There is only 50 short lines of code in between the 2 strings, and
half of those are just whitespace... so I assume I am not asking too
much with that search pattern?

Can someone (more familiar with grep) shed light on this for me?

Thanks!
-Govinda

Govinda

unread,
Sep 5, 2010, 5:34:28 PM9/5/10
to BBEdit Talk
OK, I just figured out I can do this:
search:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4\.01 Transitional//
EN"><html><head>(.*?\r)+?\[include file=/inc/ltnavbar\.inc]\r </td>

..but why does the search pattern I mention in my orig. post fail?

Thanks!
-Govinda

Patrick Woolsey

unread,
Sep 6, 2010, 11:00:15 AM9/6/10
to bbe...@googlegroups.com
Govinda <govinda.w...@gmail.com> sez:

>> I need to do a grep search/replace:
>>
>> find:
>> one string literal.. followed by any number of any chars...
>> followed by another string literal
>>

[...]


>..but why does the search pattern I mention in my orig. post fail?


The short answer :) is that (\s|\S)*? in the original pattern requires
the search to do too much work when applied against more than a small
amount of text.


The best way to match a multi-line `chunk of text` is:

(?s).+? # or ((?s).+?) if you want to preserve the matched text

where the pre-pended (?s) allows . to match line breaks, and I suggest
keeping this in the top drawer of your grep toolbox. :-)


Regards,

Patrick Woolsey
==
Bare Bones Software, Inc. <http://www.barebones.com>
P.O. Box 1048, Bedford, MA 01730-1048

Govinda

unread,
Sep 6, 2010, 1:28:42 PM9/6/10
to BBEdit Talk
Thank you Patrick!

Now I understand.

I had to search a bit, but I finally found in the manual about
Perl-Style Pattern Extensions > Pattern Modifiers
..to get the (?s) bit.

There is so much in everything grep! Thank goodness for real and
helpful humans to supplement docs! (Even though they are really
excellent docs, for a human it is still sometimes easy to miss
important things.) Thanks again!

:-)
-G
Reply all
Reply to author
Forward
Message has been deleted
0 new messages