Search and replace the content between a specific tag

254 views Asked by At
#!/usr/bin/perl
use strict;
use warnings;
my $html = q|
    <html>
    <head>
    <style>
    .classname{
        color: red;
    }
    </style>
    </head>
    <body>
    classname will have a color property.
    </body>
    </html>
|;
$html=~s/classname/NEW/g;
print $html;

This replaces classname in both places. How can I limit the replacement only to content of <body>? I'd like to see it done using HTML::Parser or HTML::TreeBuilder.

1

There are 1 answers

0
bytepusher On BEST ANSWER

I believe this does what you want, replaces classname with your regexp on all children of body element, using HTML::TreeBuilder.

I added another dummy div to input to make sure it was being processed correctly.

#!/usr/bin/perl
use strict;
use warnings;

use HTML::TreeBuilder;

my $html = q|
    <html>
    <head>
    <style>
    .classname{
        color: red;
    }
    </style>
    </head>
    <body>
    classname will have a color property.
    <div>more text with classname in it</div>
    </body>
    </html>
|;

my $tree = HTML::TreeBuilder->new_from_content($html);

replace_text( $tree->find_by_tag_name("body") );

print $tree->as_HTML."\n";

sub replace_text {

    my $html_element = shift;

    for my $el ( $html_element->content_refs_list ){

    if ( ref( $$el ) ){
        replace_text( $$el );
        next;
    }

    $$el =~ s /classname/NEW/g;

    }

    return $html_element;

}