Perl 正则表达式处理文本输入

发布于 2024-10-16 09:40:40 字数 1353 浏览 5 评论 0原文

我目前有一个 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 技术交流群。

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

发布评论

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

评论(2

南…巷孤猫 2024-10-23 09:40:40

您必须转义括号,因为它们在正则表达式中具有特殊含义。假设所有属性都用双引号引起来,且内部没有双引号,则以下内容应该有效。

$text =~ s/\[\/?\w+(\s+\w+="[^"]*")*\s*\/?\s*\]//g;

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.

$text =~ s/\[\/?\w+(\s+\w+="[^"]*")*\s*\/?\s*\]//g;
鲜血染红嫁衣 2024-10-23 09:40:40

您可以使用正则表达式仅删除方括号中的内容,并信任 HTML::TagFilter 来删除其他内容。

$text =~ s! #Start match pattern (used exclamation mark instead of / for readability)
        \[ #Left square bracket
          [^\]]*? #Followed by any character(s) which are not ]. ? means lazy match
        \] #Right square bracket
        !!gx; #Replace with nothing, globally, allow comments and whitespace

You could use regex to remove only what's in the square brackets and trust HTML::TagFilter to remove the others.

$text =~ s! #Start match pattern (used exclamation mark instead of / for readability)
        \[ #Left square bracket
          [^\]]*? #Followed by any character(s) which are not ]. ? means lazy match
        \] #Right square bracket
        !!gx; #Replace with nothing, globally, allow comments and whitespace
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文