One option is to use pull parsing. Here you have an example with HTML::TokeParser . It uses two loops, the first is used for the first appearance of your comment. It prints every tag that it finds until it appears. The second one goes through all the tags until the second comment appears and prints nothing.
The contents of script.pl :
#!/usr/bin/env perl use warnings; use strict; use HTML::TokeParser; my $p = HTML::TokeParser->new ( shift ); while ( my $token = $p->get_token ) { printf qq|%s|, $token->[0] =~ m/S|E|PI/ ? $token->[ $#$token ] : $token->[1]; if ( $token->[0] eq q|C| && $token->[1] =~ m/(?i)MyComment/ ) {
Run it like this:
perl script.pl htmlfile
This gives:
<html> <head> <title>Title</title> </head> <body> <tbody> <tr> <td width="650"> </td> </tr> </tbody> </body> </html>
Birei source share