我需要在关闭后替换文本Perl 中的标签
你们经常帮助我,伙计们,所以我希望你们可以再做一次:]
我有一个字符串,其中包含 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用正则表达式解析 HTML 通常是一个坏主意。在处理任意 HTML 的一般情况下,它尤其糟糕。然而,你越能限制你正在处理的输入,它通常会变得越来越不糟糕。
您是否尝试过使用 HTML::TreeBuilder 或 HTML::Parser 来解析您的 HTML?这个未经测试的代码应该做你想要的——据我从你的描述中可以看出,请尽可能发布示例数据和期望的结果。
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.
为什么不逐行浏览文件,计算表的所有开始/结束标记。如果总和为零(所有起始表标签都以结束标签结束),则进行匹配/替换。
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..