开发者

I need to replace text just after closed </table> tags in Perl

开发者 https://www.devze.com 2023-02-21 05:41 出处:网络
you often helped me, guys, so I hope you may do it again :] I have a string, which contains HTML Data (which doesnt necessarily have

you often helped me, guys, so I hope you may do it again :]

I have a string, which contains HTML Data (which doesnt necessarily have

<table> 

tags, but it can have then sometimes), and I want to replace a string. Actually, Im doing it li开发者_开发技巧ke "replace the first find of

<br> 

with

<br><div>newdiv</div>

", but that doesnt work when a table is done, because the "newdiv" needs to be outside the table.

Is there any way of telling Perl to replace only after a certain tag (in my case it would be

</table>

) has been found before the search?

Thanks!


Parsing HTML with regexes is generally a bad idea. It's especially bad in the generalized case of handling arbitrary HTML. However, it typically gets less and less bad the more you can restrict the inputs you are dealing with.

Have you tried using HTML::TreeBuilder or HTML::Parser to parse your HTML? This untested code should do what you want--as far as I can tell from your description, please post sample data and desired results where possible.

# Parse your html
my $t = HTML::TreeBuilder->new_from_content( $html );
$t->eof;
$t->elementify;

my @tables = $t->lookdown( _tag => 'table' );
for my $table ( @tables ) {

    # Skip this table unless it is immediately followed by a br  
    my $br = $table->right; 
    next unless $br->tag eq 'br';

    # Insert the new div
    $br->postinsert('<div>newdiv</div>');
}


why don't you go through the file line by line, counting all the start/end tags of a table. and if the sum is zero (all starting table tags are closed with an end tag) you do the matching/replacing..

0

精彩评论

暂无评论...
验证码 换一张
取 消