GREP - Find First Instance Only

673 views
Skip to first unread message

mindmojo

unread,
Jun 15, 2010, 4:12:24 PM6/15/10
to BBEdit Talk
I'm trying to insert some new HTML into several HTML pages. The new
HTML should appear between the first and second HTML-formatted
paragraphs but not between any other subsequent paragraphs.

I used grep to find the first paragraph's closing p tag, the two
returns between the paragraphs and the opening p tag of the second
paragraph. I then replace the found tags and returns with the new HTML
using multi-file search.

All this works fine EXCEPT that it replaces every instance of the
opening and closing p tags in each document.

I only want to change the first instance but each HTML page is
different enough to make it a more difficult "automated" grep task
than I originally thought.

I found an old post via Google that had several answers about finding/
replacing only the first tab in a document and ignoring subsequent
tabs in a multi-search and replace but it doesn't seem to work with my
HTML

I'd include some code but not sure how to format it for posting.

Thanks.

Roland Küffner

unread,
Jun 17, 2010, 3:17:32 AM6/17/10
to bbe...@googlegroups.com
Hi,

Am 15.06.2010 um 15, 22:12 schrieb mindmojo:
> I'm trying to insert some new HTML into several HTML pages. The new
> HTML should appear between the first and second HTML-formatted
> paragraphs but not between any other subsequent paragraphs.

I'm not quite sure if I unterstood your aims completely but you might try this pattern:

(?s)\A.+?</p>

A little explanation:
(?s): makes the dot character match line breaks, hence really any character in the document
\A: matches the beginning of the document.

So the pattern matches everything from the top of the document down to the end of the first paragraph. Just replace it with something like:

&\ryour_new_code_here

(& matches the entire search pattern without the need for brackets - something I tend to forget using myself)

Regards,
Roland

mindmojo

unread,
Jun 18, 2010, 2:19:22 PM6/18/10
to BBEdit Talk
Thanks for the suggestion. I'll try this out and report back next
week.

On Jun 17, 2:17 am, Roland Küffner <medienmeis...@googlemail.com>
wrote:

mindmojo

unread,
Jun 20, 2010, 10:07:12 AM6/20/10
to BBEdit Talk
Roland, thanks for your suggestion. With a slight modification, I got
it to do exactly what I needed.

Your suggested code found the first html-formatted paragraph but not
the first html-formatted paragraph immediately followed by a second
html-formatted paragraph. Some formatted paragraphs were inside a div
by themselves near the top of the page and I did not want to find
those.

So...

For the find, I used:
(?s)\A.+?(</p>\r)+(\r<p>)

For the replace, I used:
\1\r my replacement \r\2


On Jun 17, 2:17 am, Roland Küffner <medienmeis...@googlemail.com>
wrote:
Reply all
Reply to author
Forward
0 new messages