Perl 正则表达式处理文本输入
我目前有一个 perl 脚本,可以导入 HTML 并将其转换为纯文本。我正在使用 HTML::TagFilter 来删除所有 HTML 标签,它几乎完美地工作,只是我们遇到了一个问题。当 HTML 包含非标准 HTML 标签(例如下面示例输入中的“标题”)时,这些标签不会被删除:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam pulvinar, odio ut gravida fringilla, tellus mi ultrices felis, quis porta lacus sem ut lacus. Vestibulum massa justo, tristique id aliquet in, dapibus eu leo. Nam sapien risus, dictum et porttitor quis, egestas quis dui. Ut nec nisl felis. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.
[caption id="sample-id" align="sample-align" width="225" caption="Sample Caption"]<a href="http://www.domain.com/image.jpg"><img class="sample-image-class" title="Sample Title" src="http://www.domain.com/image.jpg" alt="Sample Alt" width="225" height="300" /></a>[/caption]
In hac habitasse platea dictumst. Duis imperdiet bibendum dolor ut ullamcorper. Suspendisse dui erat, facilisis sed aliquet non, elementum eu urna. Donec non nisi vel augue gravida imperdiet sed id tortor. Maecenas ullamcorper velit non dui imperdiet hendrerit.
我需要帮助的是一个简单的 Perl 正则表达式来完全删除此内容。我尝试了很多不同的方法,但似乎没有任何效果。我正在寻找类似以下内容的内容,它将使用括号 [] 查找并删除所有出现的非标准 HTML 标记:
$text =~ s/[(\w)+](.*)[\/(\w)+]//g;
我希望对于比我更擅长正则表达式的人来说,这是一个简单的练习。
预先感谢您的帮助!
I currently have a perl script that imports HTML and converts it to plain text. I am using HTML::TagFilter to remove all the HTML tags and it is working almost perfectly except we've run into one issue. When the HTML contains non-stand HTML tags such as the "caption" in the example input below those tags aren't being removed:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam pulvinar, odio ut gravida fringilla, tellus mi ultrices felis, quis porta lacus sem ut lacus. Vestibulum massa justo, tristique id aliquet in, dapibus eu leo. Nam sapien risus, dictum et porttitor quis, egestas quis dui. Ut nec nisl felis. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.
[caption id="sample-id" align="sample-align" width="225" caption="Sample Caption"]<a href="http://www.domain.com/image.jpg"><img class="sample-image-class" title="Sample Title" src="http://www.domain.com/image.jpg" alt="Sample Alt" width="225" height="300" /></a>[/caption]
In hac habitasse platea dictumst. Duis imperdiet bibendum dolor ut ullamcorper. Suspendisse dui erat, facilisis sed aliquet non, elementum eu urna. Donec non nisi vel augue gravida imperdiet sed id tortor. Maecenas ullamcorper velit non dui imperdiet hendrerit.
What I need help with is a simple Perl regex to remove this content completely. I've tried a bunch of different approaches but nothing seems to be working. What I'm looking for is something like the following which would find and remove all occurrences of non-standard HTML tags using brackets []:
$text =~ s/[(\w)+](.*)[\/(\w)+]//g;
I'm hoping it is a simple exercise for someone who is better at regex than I am.
Thanks in advance for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须转义括号,因为它们在正则表达式中具有特殊含义。假设所有属性都用双引号引起来,且内部没有双引号,则以下内容应该有效。
You have to escape brackets because they have special meaning within regular expressions. Assuming that all attributes will be double-quoted with no double quotes inside them, the following should work.
您可以使用正则表达式仅删除方括号中的内容,并信任 HTML::TagFilter 来删除其他内容。
You could use regex to remove only what's in the square brackets and trust HTML::TagFilter to remove the others.