Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Replacing text between two html comments

by redbeard (Beadle)
on Jul 28, 2010 at 01:11 UTC ( [id://851644]=perlquestion: print w/replies, xml ) Need Help??

redbeard has asked for the wisdom of the Perl Monks concerning the following question:

I have an html file stored as a variable $template with the following in it
<!-- MEMBERSHIPS --> . a bunch of stuff . <!-- BANQUET -->
I would like to replace all of this with something else under certain conditions. Seems simple. However, the following
$template=~s/<!-- MEMBERSHIPS -->(.*?)<!-- BANQUET -->/$paidstring/i;
where $paidstring is what I want to replace it with, seems not to work. Furthermore, I did some playing around, and found that both
/<!-- MEMBERSHIPS -->(.*?)/ and /(.*?)<!-- BANQUET -->/
will match just fine, but putting everything together shows no match. What is going on here? Is there something I am missing in attempting to search and replace text between two html comments?

Replies are listed 'Best First'.
Re: Replacing text between two html comments
by mojotoad (Monsignor) on Jul 28, 2010 at 01:59 UTC
    You want to add /s as an option on your regular expression, thereby treating the whole string as a single line. Furthermore, as you have it currently, you're also zapping your delineators without replacing them. Maybe capture them as well and include their buck-whaps ($1, $3) in the replacement expression.

    You're probably okay with this as long as you can reasonably guarantee that your comment tokens are unique within any given document. Otherwise use a real parser, such as HTML::Parser. Or use a template system like Text::Template.

    Cheers,
    Matt

Re: Replacing text between two html comments
by wfsp (Abbot) on Jul 28, 2010 at 07:08 UTC
    Another approach to this type of task is to not use comments in your HTML but to use div tags, each one having an id attribute. Using a parser you can find that div and replace it with the new content. This example uses HTML::TreeBuilder.
    #! /usr/bin/perl use strict; use warnings; use HTML::TreeBuilder; my $new_div = HTML::Element->new_from_lol( [ q{div}, {id => q{memberships}}, [q{p}, q{new stuff}] ], ); my $p = HTML::TreeBuilder->new_from_file(*DATA) or die qq{H::TB new failed\n}; my $memberships_div = $p->look_down( _tag => q{div}, id => q{memberships}, ) or die qq{memberships div not found\n}; $memberships_div->replace_with($new_div); print $p->as_HTML(undef, q{ }, {}); __DATA__ <html><head><title>replace test</title></head> <body> <p>keep</p> <p>keep</p> <div id="memberships"> <p>stuff to replace</p> </div> <div id="banquet"> <p>all about the banquet</p> </div> <p>keep</p> <p>keep</p> </body> </html>
    <html> <head> <title>replace test</title> </head> <body> <p>keep</p> <p>keep</p> <div id="memberships"> <p>new stuff</p> </div> <div id="banquet"> <p>all about the banquet</p> </div> <p>keep</p> <p>keep</p> </body> </html>
    If you haven't come across it before there is a bit of a learning curve but if you need to work with HTML it is well worth it. I avoid using regexes on HTML at all costs. Here there be dragons. :-)
Re: Replacing text between two html comments
by StommePoes (Scribe) on Jul 29, 2010 at 07:03 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://851644]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2026-05-13 22:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.