如何从Perl中的纯文本中提取URL? [英] How can I extract URLs from plain text in Perl?

查看:67
本文介绍了如何从Perl中的纯文本中提取URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过一些这样的帖子,但不完全是我想做的事情.

I've seen some posts like this, but not exactly what I want to do.

如何提取和删除URL链接,然后从纯文本中删除它们.

How can I extract and delete URL links, and then remove them from plain text.

示例:

"Hello!!, I love http://www.google.es".

我要提取"http://www.google.es",将其保存在变量中,然后将其从文本中删除.

I want extract the "http://www.google.es", save it on a variable, and then, remove it from my text.

最后,文字必须是这样的:

Finally, the text has to be like that:

"Hello!!, I love".

URL通常是文本的最后一个单词",但并非总是如此.

The URLs usually are the last "word" of the text, but not always.

推荐答案

也许您想要 URI ::查找,可以在任意文本中查找URI.您提供的代码参考中的返回值将生成URL的替换字符串,因此,如果您只想摆脱URI,则可以只返回空字符串:

Perhaps you want URI::Find, which can find URIs in arbitrary text. The return value from the code reference you give it produces the replacement string for the URL, so you can just return the empty string if you merely want to get rid of the URIs:

use URI::Find;

my $string = do { local $/; <DATA> };

my $finder = URI::Find->new( sub { '' } );
$finder->find(\$string );

print $string;

__END__
This has a mailto:joe@example.com
Go to http://www.google.com
Pay at https://paypal.com
From ftp://ftp.cpan.org download a file

这篇关于如何从Perl中的纯文本中提取URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆