我需要在关闭后替换文本Perl 中的标签

发布于 2024-10-28 02:50:40 字数 428 浏览 6 评论 0原文

你们经常帮助我,伙计们,所以我希望你们可以再做一次:]

我有一个字符串,其中包含 HTML 数据(不一定有

<table> 

标签,但有时可以有),我想替换一个字符串。 ”替换第一个发现

<br> 

实际上,我这样做就像“用

<br><div>newdiv</div>

,但是当表格完成时这不起作用,因为“newdiv”需要在表格之外。

某个标签(在我的情况下是)之后才进行替换

</table>

有没有办法告诉 Perl 仅在搜索之前找到

?谢谢!

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 like "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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光瘦了 2024-11-04 02:50:40

使用正则表达式解析 HTML 通常是一个坏主意。在处理任意 HTML 的一般情况下,它尤其糟糕。然而,你越能限制你正在处理的输入,它通常会变得越来越不糟糕。

您是否尝试过使用 HTML::TreeBuilderHTML::Parser 来解析您的 HTML?这个未经测试的代码应该做你想要的——据我从你的描述中可以看出,请尽可能发布示例数据和期望的结果。

# 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>');
}

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>');
}
挽清梦 2024-11-04 02:50:40

为什么不逐行浏览文件,计算表的所有开始/结束标记。如果总和为零(所有起始表标签都以结束标签结束),则进行匹配/替换。

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..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文